I’ve been working on a function which is players giving each other money when they’re near, but I can’t seem to get it right,
here is my code:
Server Side
TriggerClientEvent('givemoney', playerID)
Client Side
RegisterNetEvent('givemoney')
AddEventHandler('givemoney', function()
TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, PlayerPedId())
--local player = GetPlayerFromServerId(PlayerId())
local ped = GetPlayerPed(-1) <---- From
local ped2 = PlayerPedId() <----- To
local playerCoords = GetEntityCoords(ped)
local playerCoords2 = GetEntityCoords(ped2)
if( GetDistanceBetweenCoords( playerCoords, playerCoords2["x"], playerCoords2["y"], playerCoords2["z"] ) < 10)then
TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, "1")
else
TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, "2")
end
end)
this code always return “2” when I tried to give money to someone, could someone help me please? Thank you!
The actual issue is because you did not specify the x/y/z in your GetDistanceBetweenCoords() with playerCoords. Vdist is just another way to get the distance.
Thank you guys so much, but I’ve encountered a new problem, the code now always return “1”, no matter how far me and the other player are, is there anything wrong with passing the PlayerID from the code? Thank you!
Here is the full Server code
AddEventHandler('chatMessage', function(source, n, message)
local args = stringsplit(message, " ")
if (args[1] == "/givemoney") then
CancelEvent()
if (args[2] ~= nil) then
local playerID = tonumber(args[2])
end
TriggerClientEvent('givemoney', playerID)
end
end)
Clien Code now
RegisterNetEvent('givemoney')
AddEventHandler('givemoney', function()
TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, PlayerPedId())
--local player = GetPlayerFromServerId(PlayerId())
local ped = GetPlayerPed(-1) <---- From
local ped2 = PlayerPedId() <----- To
local playerCoords = GetEntityCoords(ped)
local playerCoords2 = GetEntityCoords(ped2)
if(Vdist( playerCoords.x, playerCoords.y, playerCoords.z, playerCoords2.x, playerCoords2.y, playerCoords2.z) < 10.0)then
TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, "1")
else
TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, "2")
end
end)
What do you mean by passing the argument in AddEventHandler ? Cause I want to get the distance for a cuff system
Thanks !
Here is the code :
– client side handcuff script
local handCuffed = false – store wether we are handcuffed or not
RegisterNetEvent(‘mHandCuff’) – register that this is a valid event
– register a function to be called when we recieve the event
AddEventHandler(‘mHandCuff’, function(PlayerPedId)
local ped = GetPlayerPed(-1) ---- From
local ped2 = PlayerPedId() ----- To
local playerCoords = GetEntityCoords(ped)
local playerCoords2 = GetEntityCoords(ped2)
if(Vdist( playerCoords.x, playerCoords.y, playerCoords.z, playerCoords2.x, playerCoords2.y, playerCoords2.z) < 2.0)then
– set handCuffed equal to the oposite of its current value (true or false)
handCuffed = not handCuffed
end
end)
Citizen.CreateThread(function() – create a thread
while true do – loop through this code infinitely
Citizen.Wait(1) – required in an infinite loop or it will crash
if (handCuffed == true) then -- we are handcuffed
RequestAnimDict('mp_arresting') -- tell the game to load this animation dictionary
-- check if the animation dictionary has loaded, if not, wait
while not HasAnimDictLoaded('mp_arresting') do
Citizen.Wait(0)
end
local myPed = PlayerPedId() -- get our ped identifier
local animation = 'idle' -- animation to play
local flags = 49 -- only play the animation on the upper body
-- play the animation
TaskPlayAnim(myPed, 'mp_arresting', animation, 8.0, -8, -1, flags, 0, 0, 0, 0)
end