Drag/Grab system problem

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)

server.lua

RegisterCommand("grab", function(source, args, rawCommand)
    local src = source
	TriggerClientEvent("Grab", src)
end, false)

Do you have any idea? :frowning:

Where is the function GetPedInFront()?

Under. I haven’t include the entire plugin and i’ve forgot this

function GetPedInFront()
	local player = PlayerId()
	local plyPed = GetPlayerPed(player)
	local plyPos = GetEntityCoords(plyPed, false)
	local plyOffset = GetOffsetFromEntityInWorldCoords(plyPed, 0.0, 1.3, 0.0)
	local rayHandle = StartShapeTestCapsule(plyPos.x, plyPos.y, plyPos.z, plyOffset.x, plyOffset.y, plyOffset.z, 1.0, 12, plyPed, 7)
	local _, _, _, _, ped = GetShapeTestResult(rayHandle)
	return ped
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

on your client

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)




on your server

RegisterServerEvent("grabThisDude")
AddEventHandler("grabThisDude", function(callthis)
    TriggerClientEvent("grabProcess", callthis, source)
end)

same problem :frowning:

i don’t know bro, there might something wrong, but this is working for me

is the

return the true Id of the targetplayer that you’ve get from getPedInFront?

or does getPedInFront return what you want?

See what I’ve tested

and then

it says PED Found, but it doesn’t work. I don’t know why.

My mistake, Add this function and change … below

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)

Thanks, now it works, but i can’t undrag him :joy:

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

It says “No player nearby”

So it can’t find the ped when attached :confused:

try to increase the length check in your getpedinfrontbro() that’s the way, increase it to 1.8 or more

targetplayer = 0 now :frowning:

I’ve tried everything but it still doesn’t work :confused:

Just wondering if you ever got this working with the detach?