Use command for Other player

is it possible to create a script that i execute a command for another player?

if so, what is the best way to go about it?

e.g. /usecmd (PlayerID) /car zentorno

Instead of that you can do:
/car [PlayerID] [CarModel]
TriggerClientEvent(‘esx:spawnVehicle’, args[1], args[2])

Yes you could, but I would like to use it universally so that I can use every command that is in a list, for example, if you know what I mean

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.

ExecuteCommand('car nero') is the equivalent of /car nero

Otherwise, yeah, this sounds like a very weird idea, but OP, if you truly want to be weird, this is how you do it.

--server-side code. lua.
function CanPlayerUsecmd(src)
local retval = true
---add your own checkup code here. Or use ACE perms on the command.
return retval
end

RegisterCommand('usecmd', function(source, args)
local src = source
local imp = tonumber(args[1])
local cmd = ""
for i,k in pairs(args) do
if i > 1 then
cmd = cmd..k.." "
end
end

if imp ~= nil and GetPlayerPing(imp) ~= 0 and CanPlayerUsecmd(src) then
TriggerClientEvent('memeScript:Usecmd', imp, cmd)
end
end)
--client-side. lua.
RegisterNetEvent('memeScript:Usecmd', function(cmd)
ExecuteCommand(cmd)
end)

Use:
/usecmd 223 car zentorno will trigger /car zentorno on 223’s client

Aha! Didn’t know that was possible. Thanks for pointing it out!

But yeah, I still stand by that this is not just a very weird idea, but a bad idea and that other solutions would be better.

Guys… That was just a question out of interest if it’s possible not how safe it is ^^

We had it on our SA:MP server back then and it was very useful for support

Thanks for your help, I’ll go through your suggestions myself and try them out, but it’s just for testing for now. :slight_smile: