Hello, I am new to this platform and I would like to start scripting on FiveM as well. I’m used to the scripting on Multi Theft Auto, but it seems very close to the one on FiveM, only that here aren’t really any explicit examples in the documentation on how you could do something.
For example I want to do something like:
function SetHealth(theTarget, theHealth)
local health = theHealth
local theTarget = (What can I put here to get an id or player name as target)
if health == tonumber(health) then
SetEntityHealth(theTarget, health)
end
end
I hope someone understand what I’m trying to do
Hi, first thing first, I recommand you to look at the native webiste : Native Reference - Cfx.re Docs
You can finds everything you need there, you also got a search bar
and for getting the right ped I sugest you to use PlayerPedId() wich is the local ped.
If you want to send this as an event to another player, use a serversided event, and send it to the right player, and hes gonna heal himslef in client side, if you need any further help just let me know
I know about Native Reference - Cfx.re Docs but I meant that there are not really examples of scripts in the documentation, only what parameters you have to use
Anyway, I’m gonna try to do what you said, thx for answering.
RegisterNetEvent("SetHealth")
AddEventHandler("SetHealth", function(theHealth)
local health = theHealth
local theTarget = PlayerPedId()
SetEntityHealth(theTarget, health)
end)
This would be the client side function to set health on a player, the if EntityHealth == health part is not need since you want to set the health
And this is how it should be in server-side?
RegisterCommand("sethealth", function(health)
TriggerClientEvent("SetHealth", source, health)
end, false)
Depends on how you want you’re script working, if you want everyone to use the commands then you can put the command client side, or else if you weant only admin to use it it should look like this:
RegisterCommand("sethealth", function(args)
TriggerClientEvent("SetHealth", args[1], args[2])
end, "admin")
And you can use it like /sethealth 6 200 6 is the player server id and 200 is max health
1 Like
Thanks, this is what I wanted to do
1 Like
For the players who want to do the same I will let the script here:
– Client
RegisterNetEvent("SetHealth")
AddEventHandler("SetHealth", function(theTarget, theHealth)
local health = theHealth
local playerIdx = GetPlayerFromServerId(theTarget)
local ped = GetPlayerPed(playerIdx)
SetEntityHealth(ped, tonumber(health))
end)
– Server
RegisterCommand("sethealth", function(player, args)
local theTarget, theHealth = args[1], args[2]
if theTarget == "" or theTarget == nil then
print("You need to specify the player ID")
else
TriggerClientEvent("SetHealth", player, theTarget, theHealth)
end
if theHealth == "" or theHealth == nil then
print("You need to specify the health value")
else
TriggerClientEvent("SetHealth", player, theTarget, theHealth)
end
end, "admin")
I’ved look at you’re solution and I think there might be something not working, or its might just be me but when you trigger the client event you use player, but player is the source wich mean you cast the Trigger on the player who use the command.
I’ll do it like this:
-Client
RegisterNetEvent("SetHealth")
AddEventHandler("SetHealth", function(theHealth)
local health = theHealth
local ped = PlayerPedId()
SetEntityHealth(ped, tonumber(health))
end)
RegisterCommand("sethealth", function(player, args)
local theTarget, theHealth = args[1], args[2]
if theTarget == "" or theTarget == nil then
print("You need to specify the player ID")
elseif theHealth == "" or theHealth == nil then
print("You need to specify the health value")
else
TriggerClientEvent("SetHealth", theTarget, theHealth)
end
end, "admin")
Because the way you have done it, may works, but its not a good to code, you we’re telling a client to set the health of another client insted of telling the client to set himsleft health.
Hope this would help, if its not working just tell and I’ll try to provide more help
1 Like