Today I did my first script ever, a very easy one and it is functional client-side but then I wanted to add IsPlayerAceAllowed check and I saw that it is a server-side native so I thought I’d try to convert all of it server-side but I didn’t succeed to.
**Client-Side - Working perfectly**
RegisterCommand('setarmour', function(source, args, rawCommand)
local ped = GetPlayerPed(GetPlayerFromServerId(args[1]))
local amount = tonumber(args[2])
if args[1] and args[2] then
SetPedArmour(ped, amount)
TriggerEvent("chat:addMessage", {
args = {"Armour of ID " .. (args[1]) .. " was set to " .. (args[2]) .. " "}
})
else
TriggerEvent("chat:addMessage", {
args = {"Syntax: /setarmour ID Amount"}
})
end
end)
Then, I tried to convert it like this:
**Client.lua after converting (not working)**
RegisterNetEvent('ac:setarmour')
AddEventHandler('ac:setarmour', function(args)
local ped = GetPlayerPed(GetPlayerFromServerId(args[1]))
local amount = tonumber(args[2])
SetPedArmour(ped, amount)
TriggerEvent("chat:addMessage", {
args = {"Armour of ID " .. (args[1]) .. " was set to " .. (args[2]) .. " "}
})
else
TriggerEvent("chat:addMessage", {
args = {"Syntax: /setarmour ID Amount"}
})
end
end)
**Server.lua (not working)**
RegisterCommand('setarmour', function(source, args, rawCommand)
if args[1] and args[2] and IsPlayerAceAllowed(source, "admincommands") then
TriggerClientEvent('ac:setarmour', source, args)
else
print("You're not an admin.")
end
end)
Maybe someone can show me an example how should I have converted it succesfully. Thanks!
RegisterNetEvent('ac:setarmour')
AddEventHandler('ac:setarmour', function(args)
local ped = GetPlayerPed(GetPlayerFromServerId(args[1]))
local amount = tonumber(args[2])
if args then --this
SetPedArmour(ped, amount)
TriggerEvent("chat:addMessage", {
args = {"Armour of ID " .. (args[1]) .. " was set to " .. (args[2]) .. " "}
})
else
TriggerEvent("chat:addMessage", {
args = {"Syntax: /setarmour ID Amount"}
})
end
end)
Thank you @rbsace
However, now I’ve ran into another problem:
If I do /setarmour (unconnected ID) (amount) for some reason it still sets my armour. Should I add a check if the player is connected or is my code broken somewhere and no matter what I do it will always set only my armour?
Thank you @rbsace
Works flawlessly now but I’d like to ask how exactly does this work: (‘ac:setarmour’, args[1], args)
I know why [args1] is there for but what about args? What if I’d put args[2] in there? (‘ac:setarmour’, args[1], args[2]) would it be the same thing? I struggle to understand how this works and I just found out that source is when you want the command executed on who typed it…
Same thing for AddEventHandler(‘ac:setarmour’, function(args), I’ve seen some examples and decided to leave function(args) as it is but I don’t know what I did there to be honest. And same for:
RegisterCommand(‘setarmour’, function(source, args, rawCommand) I put source, args, and rawCommand just because I saw an example on FiveM scripting but I don’t know why did I really put them there…
Do you have any tutorial to understand these better?
Thanks
the second argument in triggerclientevent is a player id, so when you use arg/argument in registercommand it is set in the args table, so args[1] is the second string after the first space and args[2] is the string after the second space and so on (/command a b) a is args[1] and b is args[2], so if you set args[2] in triggerclientevent it would send to the id 100 with 1 armour if you use ] /setarmour 100 1 ] About the tutorials, I think the ones in fivem docs page is the best to use