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)