[Release]Drag command

Done with the help with everyone who contributed to this
Credits to:
@Valk for solving the last piece of the puzzle
@Michael_Sanelli for the initial template
@NYKILLA1127 for mentioning the AttachEntityToEntity native
I just made it so you can detach people and so that you cannot drag yourself.

drag.rar (966 Bytes)

Client
local draggedBy = -1
local drag = false
local wasDragged = false

RegisterNetEvent("drag")
AddEventHandler("drag", function(_source)
    draggedBy = _source
    drag = not drag
end)

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
        if drag then
            wasDragged = true
            AttachEntityToEntity(PlayerPedId(), 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(PlayerPedId()) and wasDragged then
                wasDragged = false
                DetachEntity(PlayerPedId(), true, false)    
            end
        end
    end
end)
Server
RegisterCommand("drag", function(source, args, rawCommand)
	if #args == 1 then
		if GetPlayerName(args[1]) then
			if args[1] ~= source then
				TriggerClientEvent("drag", args[1], source)
			else
				TriggerClientEvent("chatMessage", source, "Error: ", {255, 0, 0}, "You cannot drag yourself.")
			end
		else
			TriggerClientEvent("chatMessage", source, "Error: ", {255, 0, 0}, "/drag [id]")
		end
	else
		TriggerClientEvent("chatMessage", source, "Error: ", {255, 0, 0}, "/drag [id]")
	end
end, false)

Drag the closest player (11/05/19)
drag.zip (1.6 KB)

Client
local Drag = {
	Distance = 3,
	Dragging = false,
	Dragger = -1,
	Dragged = false,
}

function Drag:GetPlayers()
	local Players = {}
    
	for Index = 0, 255 do
		if NetworkIsPlayerActive(Index) then
			table.insert(Players, Index)
		end
	end

    return Players
end

function Drag:GetClosestPlayer()
    local Players = self:GetPlayers()
    local ClosestDistance = -1
    local ClosestPlayer = -1
    local PlayerPed = PlayerPedId()
    local PlayerPosition = GetEntityCoords(PlayerPed, false)
    
    for Index = 1, #Players do
    	local TargetPed = GetPlayerPed(Players[Index])
    	if PlayerPed ~= TargetPed then
    		local TargetCoords = GetEntityCoords(TargetPed, false)
    		local Distance = #(PlayerPosition - TargetCoords)

    		if ClosestDistance == -1 or ClosestDistance > Distance then
    			ClosestPlayer = Players[Index]
    			ClosestDistance = Distance
    		end
    	end
    end
    
    return ClosestPlayer, ClosestDistance
end

RegisterNetEvent("Drag")
AddEventHandler("Drag", function(Dragger)
	Drag.Dragging = not Drag.Dragging
	Drag.Dragger = Dragger
end)

RegisterCommand("drag", function(source, args, fullCommand)
	local Player, Distance = Drag:GetClosestPlayer()

	if Distance ~= -1 and Distance < Drag.Distance then
		TriggerServerEvent("Drag", GetPlayerServerId(Player))
	else
		TriggerEvent("chat:addMessage", {
			color = {255, 0, 0},
			multiline = true,
			args = {"Drag", "Please get closer to the target!"},
		})
	end
end, false)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)

		if NetworkIsSessionStarted() then
			TriggerEvent("chat:addSuggestion", "/drag", "Toggle drag the closest player")
			return
		end
	end
end)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		if Drag.Dragging then
			local PlayerPed = PlayerPedId()

			Drag.Dragged = true
			AttachEntityToEntity(PlayerPed, GetPlayerPed(GetPlayerFromServerId(Drag.Dragger)), 4103, 11816, 0.48, 0.00, 0.0, 0.0, 0.0, 0.0, false, false, false, false, 2, true)
		else
			if Drag.Dragged then
				local PlayerPed = PlayerPedId()

				if not IsPedInParachuteFreeFall(PlayerPed) then
					Drag.Dragged = false
					DetachEntity(PlayerPed, true, false)    
				end
			end
		end
	end
end)
Server
RegisterServerEvent("Drag")
AddEventHandler("Drag", function(Target)
	local Source = source
	TriggerClientEvent("Drag", Target, Source)
end)
30 Likes

Already use this on police cops 0.1.3 (not already released) but thanks for sharing anyway ^^

EDIT : nvm, mine is bugged, is there any problem if I take this and integrate it directly on Cops_FiveM ? (with credits ofc)

2 Likes

Scripts are meant to be shared and used, go for it.

15 Likes

i love this community at times!

2 Likes

Can i please bear hug you?

1 Like

+1 Open mouth kiss!!! haha thanks guys

Will you be making a way to let people go after you drag them?

you need a command to undrag

Just type the command with the same id to undrag

From my experience using that command again will not do anything.

Having issues with perms :confused:
Im a Cop and my job id is 2, Also perm level 1000 and im the owner.

I have tried these with no luck:
elseif playerJob ~= “Cop” and playerJob ~= “Recruit” and playerJob ~= “Police” and playerJob ~= “2” then

Also tried “cop” “recruit” “police”

Am i being dumb?

That only works on my server, you have to make the permission check yourself.

And how would one do that and where?

This is basic knowledge, if it’s your server I suggest you learn yourself some coding.

You could either get into Lua or C# as a start. There are several usefull links scattered around on the forums

We have a Wiki you could take a look at also take a look at NativeDB for usefull natives.

If you are not willing to learn there is no point in running a server.

:snail:

2 Likes

thannks amazing work. it works to drag just have to get that Undrag command to work because it will not stop making u drag with the same command so have to look that one up gone see if can make it work somehow @Frazzle

I’ve just fixed my error.

1 Like

@Frazzle yeah just saw it you work fast man damn good job gone try it right now many thanks!

so the permission is that connected to the job id system?

Permissions/groups is related to Essentialmode. To integrate the job ID system you would have to write it custom to your needs.

Great script! Thanks for sharing!