Hi at all,
I’m developing a grab system for my server, but it doesn’t work. I’ve tried all. Here is the code
client.lua
local drag = false
local wasDragged = false
RegisterNetEvent("Grab")
AddEventHandler("Grab", function(_source)
draggedBy = _source
drag = not drag
local player = PlayerId()
local plyPed = GetPlayerPed(player)
local targetped = GetPedInFront()
if targetped ~= nil and targetped ~= 0 then
if drag then
wasDragged = true
AttachEntityToEntity(targetped, GetPlayerPed(GetPlayerFromServerId(draggedBy)), 4103, 11816, 0.48, 0.00, 0.0, 0.0, 0.0, 0.0, false, false, false, false, 2, true)
else
if not IsPedInParachuteFreeFall(targetped) and wasDragged then
wasDragged = false
DetachEntity(targetped, true, false)
end
end
else
ShowNotification('Non ci sono giocatori vicini')
end
end)
Try to Citizen.Trace() to check if targetedtped is always return a nil/0 or not
and why don’t you just
--RegisterNetEvent("Grab")
AddEventHandler("Grab", function()
Citizen.Trace("Event was Triggered properly") -- this should have show in F8
--draggedBy = _source --useless
drag = not drag
--local player = PlayerId() --useless
--local plyPed = GetPlayerPed(player) --useless
local targetped = GetPedInFront() -- check this [ NOT SURE IF IT'S WORK]
if targetped ~= nil and targetped ~= 0 then
Citizen.Trace("Found PED") -- check this in F8 [ iF it's not shown, then your GetPedInFront is fucked up ]
if drag then
wasDragged = true
AttachEntityToEntity(targetped, GetPlayerPed(-1), 4103, 11816, 0.48, 0.00, 0.0, 0.0, 0.0, 0.0, false, false, false, false, 2, true)
else
if not IsPedInParachuteFreeFall(targetped) and wasDragged then
wasDragged = false
DetachEntity(targetped, true, false)
end
end
else
ShowNotification('Non ci sono giocatori vicini')
end
end)
-- register the command on client-side , no need to do in on server-side
RegisterCommand("grab", function(source, args, rawCommand)
TriggerEvent("Grab")
end)
Only on client-side, no need to be done it on server if you just want to get the ped-in front , not by entering their ID
I’ve already test it with print and shownotification. It goes
The “AttachEntityToEntity(targetped, GetPlayerPed(-1))” apparently work with AttachEntityToEntity(GetPlayerPed(), targetplayer) (that is stupid, i don’t want it…)
this is completely what you want, you need to make a variable store the dragger id, and use this in the thread by trigger The attach, let me write you purpose fix , wait 5 min
local draggerId = nil
local isDrag = false
local stillDrag = false
Citizen.CreateThread(function()
while true do
Citizen.Wait(5)
if isDrag then
local ped = GetPlayerPed(GetPlayerFromServerId(draggerId))
local myped = PlayerPedId()
AttachEntityToEntity(myped, ped, 4103, 11816, 0.48, 0.00, 0.0, 0.0, 0.0, 0.0, false, false, false, false, 2, true)
stillDrag = true
else
if stillDrag and not IsPedInParachuteFreeFall(myped) then
DetachEntity(PlayerPedId(), true, false)
stillDrag = false
end
end
end
end)
AddEventHandler("Grab", function()
local targetped = GetPedInFront() -- check this [ NOT SURE IF IT'S WORK]
if targetped ~= nil and targetped ~= 0 then
Citizen.Trace("Found PED") -- check this in F8 [ iF it's not shown, then your GetPedInFront is fucked up ]
TriggerServerEvent("grabThisDude", GetPlayerServerId(targetped))
else
ShowNotification('Non ci sono giocatori vicini')
end
end)
-- register the command on client-side , no need to do in on server-side
RegisterCommand("grab", function(source, args, rawCommand)
TriggerEvent("Grab")
end)
RegisterNetEvent("grabProcess")
AddEventHandler("grabProcess", function(hisId)
draggerId = hisId
isDrag = not isDrag
end)
Because your GetPedInFront already returns a playerPed, so we need to reverse it back to the playerId
function GetPlayerFromPed(ped)
for a = 0, 64 do
if GetPlayerPed(a) == ped then
return a
end
end
return -1
end
AND CHANGE!!!
AddEventHandler("Grab", function()
local targetped = GetPedInFront() -- check this [ NOT SURE IF IT'S WORK]
if targetped ~= nil and targetped ~= 0 then
Citizen.Trace("Found PED") -- check this in F8 [ iF it's not shown, then your GetPedInFront is fucked up ]
TriggerServerEvent("grabThisDude", GetPlayerServerId(targetped))
else
ShowNotification('Non ci sono giocatori vicini')
end
end)
to
AddEventHandler("Grab", function()
local targetped = GetPedInFront() -- check this [ NOT SURE IF IT'S WORK]
if targetped ~= nil and targetped ~= 0 then
Citizen.Trace("Found Ped")
local pedPlayer = GetPlayerFromPed(targetped) -- get player client id from ped
TriggerServerEvent("grabThisDude", GetPlayerServerId(pedPlayer))
else
ShowNotification('Non ci sono giocatori vicini')
end
end)
Thats maybe your function can’t find the ped , becausw its specific too close, but the length of attach is far away than 1.0 , try to check if it’s return a true ped server id or a nil