First of all, I would advise against executing commands for other players due to security reasons. And even if you added all the checks needed to make something like this “secure”. And second I don’t think it even is possible. ExecuteCommand()
only takes a string as an argument, which means that you can’t for example specify the car you want to spawn.
Instead of executing commands on other players, I would advise you to add commands on the server-side that trigger events on the specified client.
For example:
Server:
args[1] = server id of player
args[2] = vehicle
RegisterCommand('spawnCarOnClient', function(source, args, rawCommand)
TriggerClientEvent('client:spawnCar', tonumber(args[1]), args[2])
end, true)
Client:
RegisterNetEvent('client:spawnCar')
AddEventHandler('client:spawnCar', function(car)
-- Code that spawns the vehicle.
end)
This example would use fivem’s ace system to only allow admins etc. to use the command.
If you want to learn more about the ace system read this guide: Basic Aces & Principals overview/guide.