Model spawner by chat

So I’m trying to make a quick script that changes your player model by a chat command.
Right now I want to make it change model to “s_m_y_sheriff_01”, but it does not work.

client script:


server script:


Some help would be appreciated.

First of all, don’t call SetPlayerModel in a loop. I don’t know what will happen if you do but, I can assure you it’s not going to end well for the client. Instead, put the code in the event you’re calling (once the model is set, it’s set).

As for changing the player’s model you need to request it and make sure it’s loaded before showing it to the player.

You would have to do something like

Citizen.CreateThread(function()
    RequestModel(model)
    while not HasModelLoaded(model) do -- Wait for model to load
        RequestModel(model) -- Keep requesting it
        Citizen.Wait(0) -- Needed 
    end

    SetPlayerModel(PlayerId(), model) 
    SetModelAsNoLongerNeeded(model)
end)

You need to register the event as a net event so that it can be triggered by the server then, put the Citizen.CreateThread stuff in the event handler, like so

RegisterNetEvent("Sheriff") -- Allows the event to be triggered from server
AddEventHandler("Sheriff", function()
    Citizen.CreateThread(function() -- Allows us to request model and stuff without causing client to hang
    
    end)
end)

Then, you can add the model logic to the thread. Like so

RegisterNetEvent("Sheriff") -- Allows the event to be triggered from server
AddEventHandler("Sheriff", function()
    Citizen.CreateThread(function() -- Allows us to request model and stuff without causing client to hang
        RequestModel(model)

        while not HasModelLoaded(model) do
            RequestModel(model)
            Citizen.Wait(0)
        end
        
        SetPlayerModel(player, model) 
        SetModelAsNoLongerNeeded(model)
    end)
end)