Police Tackling

That is not a built-in fivem native.

Here is a function for it:

function GetClosestPlayer()
    local players = GetPlayers()
    local closestDistance = -1
    local closestPlayer = -1
    local ply = GetPlayerPed(-1)
    local plyCoords = GetEntityCoords(ply, 0)

    for index,value in ipairs(players) do
      local target = GetPlayerPed(value)
      if(target ~= ply) then
        local targetCoords = GetEntityCoords(GetPlayerPed(value), 0)
        local distance = GetDistanceBetweenCoords(targetCoords['x'], targetCoords['y'], targetCoords['z'], plyCoords['x'], plyCoords['y'], plyCoords['z'], true)
        if(closestDistance == -1 or closestDistance > distance) then
          closestPlayer = value
          closestDistance = distance
        end
      end
    end
    return closestPlayer, closestDistance
end
1 Like

where do i put that code in?

1 Like

Bottom of your client file.

Almost forgot! You need this above that function too:

function GetPlayers()
    local players = {}

    for i = 0, 31 do
        if NetworkIsPlayerActive(i) then
            table.insert(players, i)
        end
    end

    return players
end

whis these to codes, will the tackling work?

I dont have the opertunity to test it so?

1 Like

I believe that will just make yourself ragdoll.

make myself ragdoll, not the purp the policeman is chasing?

I want to try to create something that the guy is chasing the otherguy does some kind of jump at him or something, and the player will ragdoll, when the guy stands up he will be in cuffs.

so is this how it is suposed to look?

---------------------------- Resources -----------------------------------------------
server_script "server.lua"
client_script "client.lua"


---------------------------- Server --------------------------------------------------
 
RegisterServerEvent('Tackle')
AddEventHandler('Tackle', function(player)
    TriggerClientEvent("tackleragdoll", player)
end)

---------------------------- Client --------------------------------------------------
 
 Citizen.CreateThread(
    function()
        while true do
            Citizen.Wait(0)
            if (IsDisabledControlPressed(1, 32) and IsDisabledControlJustPressed(1, 38)) then
                if IsControlPressed(1, 303) or IsControlPressed(1, 38) and GetLastInputMethod( 0 ) then
                if IsPedInAnyVehicle(GetPlayerPed(-1)) then
                else
                    local player, distance = GetClosestPlayer()
                    if distance ~= -1 and distance <= 1.5 then
                        TriggerServerEvent("Tackle", GetPlayerServerId(player))
                    end
                    SetPedToRagdoll(GetPlayerPed(-1), 1000, 1000, 0, 0, 0, 0)
                end
            end
        end
    end
)
 
RegisterNetEvent("tackleragdoll")
AddEventHandler(
    "tackleragdoll",
    function()
        SetPedToRagdoll(GetPlayerPed(-1), 5000, 5000, 0, 0, 0, 0)
    end
)

function GetPlayers()
    local players = {}

    for i = 0, 31 do
        if NetworkIsPlayerActive(i) then
            table.insert(players, i)
        end
    end

    return players
end

function GetClosestPlayer()
    local players = GetPlayers()
    local closestDistance = -1
    local closestPlayer = -1
    local ply = GetPlayerPed(-1)
    local plyCoords = GetEntityCoords(ply, 0)

    for index,value in ipairs(players) do
      local target = GetPlayerPed(value)
      if(target ~= ply) then
        local targetCoords = GetEntityCoords(GetPlayerPed(value), 0)
        local distance = GetDistanceBetweenCoords(targetCoords['x'], targetCoords['y'], targetCoords['z'], plyCoords['x'], plyCoords['y'], plyCoords['z'], true)
        if(closestDistance == -1 or closestDistance > distance) then
          closestPlayer = value
          closestDistance = distance
        end
      end
    end
    return closestPlayer, closestDistance
end

That should work :smiley:

1 Like

I made some people on my server to check it out, but it didnt work when they ran after someone and pressing W and E, do you know why?

Idk your code is quite messy. Give me a minute.

__resource.lua

client_script 'client.lua'
server_script 'server.lua'

client.lua

local pP = GetPlayerPed(-1)

Citizen.CreateThread(function()
  while true do
    Citizen.Wait(0)
    if IsControlPressed(0, 51) then
      if IsPedInAnyVehicle(pP) then
        TriggerEvent('chatMessage', 'Tackle', {255, 255, 255}, 'You cannot tackle someone in a vehicle')
      else
        local player, distance = GetClosestPlayer()
        local closestID = GetPlayerServerId(player)
        if distance ~= -1 and distance <= 1.5 then
          TriggerServerEvent('tackleServer', closestID)
        end
        SetPedToRagdoll(pP, 1000, 1000, 0, 0, 0, 0)
      end
    end
  end
end)

RegisterNetEvent('tackleClient')
AddEventHandler('tackleClient', function()
  local pP = GetPlayerPed(-1)
  SetPedToRagdoll(pP, 5000, 5000, 0, 0, 0, 0)
end)

function GetPlayers()
    local players = {}

    for i = 0, 31 do
        if NetworkIsPlayerActive(i) then
            table.insert(players, i)
        end
    end

    return players
end

function GetClosestPlayer()
    local players = GetPlayers()
    local closestDistance = -1
    local closestPlayer = -1
    local ply = GetPlayerPed(-1)
    local plyCoords = GetEntityCoords(ply, 0)

    for index,value in ipairs(players) do
      local target = GetPlayerPed(value)
      if(target ~= ply) then
        local targetCoords = GetEntityCoords(GetPlayerPed(value), 0)
        local distance = GetDistanceBetweenCoords(targetCoords['x'], targetCoords['y'], targetCoords['z'], plyCoords['x'], plyCoords['y'], plyCoords['z'], true)

        if(closestDistance == -1 or closestDistance > distance) then
          closestPlayer = value
          closestDistance = distance
        end
      end
    end

    return closestPlayer, closestDistance
end

server.lua

RegisterServerEvent('tackleServer')
AddEventHandler('tackleServer', function(closestID)
    TriggerClientEvent("tackleragdoll", closestID)
end)

I will post this as a release if it works.

1 Like

what buttons is it set to? W and E together

Just E.

But if your running with W and shift, it wont work or?
Or is it set on some kind of timer?

is it possible to talk to you on discord maby?

My discord is Woopie#6431 if you want to add me!

I will most likely try and do the script when I get home.

1 Like

:smiley:

1 Like

Hello good ones, a question how can I do so that only those who are police can do it, because I don’t want civilians to do it.

Dude?! Bumping 4 years old post… Insane!!!

Look at this since it has been made with config for police only : Search · ktackle · GitHub