Help with a cuff nearest player!

Hey ya’ll! I have this code (see below) to cuff nearest player. However, as you can guess it’s not working! The code is going through, as I have received the notification, but the cuff aspect isn’t working. I am not receiving any errors either. Any help is godly. Thanks

RegisterNetEvent('cuffp')
AddEventHandler('cuffp', function()
        closest, distance = GetClosestPlayer()
        if closest ~= nil and DoesEntityExist(GetPlayerPed(closest)) then
            if distance -1 and distance < 3 then
                drawNotification('~g~ SUCCESS: ~w~You cuffed the nearest player. (' .. GetPlayerName(closest) .. ')')    
                local closestID = GetPlayerServerId(closest)
                TriggerServerEvent('cuffServer', closestID)
            else
                drawNotification("~r~ ERROR: ~w~Nearest Player is too far away!")    
        end
    end
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 = Vdist(targetCoords["x"], targetCoords["y"], targetCoords["z"], plyCoords["x"], plyCoords["y"], plyCoords["z"])
            if(closestDistance == -1 or closestDistance > distance) then
                closestPlayer = value
                closestDistance = distance
            end
        end
    end
    
    return closestPlayer, closestDistance
end

Hello, this is a friendly reminder because this is your first time creating a topic (or it has been a while since your last topic) in this category.

Please note that most of the support is provided by the FiveM community on a voluntary basis. We ask you to be patient; there is no guarantee we have a solution to your problem(s). To avoid unnecessary/duplicate topics, please browse the forums before creating a topic.

To improve your chances of your issue(s) being solved, please provide as much information as possible about the issue(s) you are having. Also —whenever possible— please use the template given to you when creating a topic.

Thanks for keeping these forums tidy!
:mascot:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.