Attempt to call a nil value

Hi everyone. I am currently coding a special revive script that allows regular players to revive each other if there are no EMS on.

This is the code I have in my “previve-s.lua” (server-sided script):

ESX=nil
Citizen.CreateThread(function()
    while ESX==nil do
        TriggerEvent('esx:getSharedObject',function(obj) ESX=obj end)
        Citizen.Wait(0)
    end
end)

RegisterCommand("previve", function()
    local ped=GetPlayerPed(-1)
    local playerCoords=GetEntityCoords(ped)
    local cplayer,cdistance=GetClosestPlayer()
    local playerCoords2=GetEntityCoords(ped2)
                    if EmsCount==0 and cdistance<10 and IsPedDeadOrDying(cplayer,1) then
                        TaskStartScenarioInPlace(ped,'CODE_HUMAN_MEDIC_TEND_TO_DEAD',0,true)
                        Citizen.Wait(7000)
                        TriggerClientEvent("revive",ped2)
                        ClearPedTasks(ped) 
                   
                    elseif EmsCount>0 then
                        Notif("There are EMS on! You cannot revive this player. Contact EMS for help!")
                    
                    elseif cdistance>10 and IsPedDeadOrDying(cplayer,1) then
                        Notif("Get closer to the player.")
                   
                    elseif not IsPedDeadOrDying(cplayer,1) then
                       Notif("The player isn't dead! Why are you even trying?")
                    end
    end)


RegisterNetEvent("revive")
AddEventHandler("revive", function(k)
	local plyCoords = GetEntityCoords(k)
	ResurrectPed(k)
    ClearPedBloodDamage(k)
	SetEntityHealth(k, 200)
	ClearPedTasksImmediately(k)
	SetEntityCoords(k, plyCoords.x, plyCoords.y, plyCoords.z + 1.0, 0, 0, 0, 0)
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(target, 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


And this is the code I have in the “previve-c.lua”:

function Notif(msg)
	SetNotificationTextEntry("STRING")
	AddTextComponentString(msg)
	DrawNotification(true, false)	
end

Citizen.CreateThread(function()
    EmsCount=0
    for _, player in ipairs(GetActivePlayers()) do
        Citizen.Wait(5)
        local ped1 = GetPlayerPed(player)
        if ped1.job.name=='ambulance' then
            EmsCount=EmsCount+1
        end
    end
    return EmsCount
end)


The error I am getting from the console when I try to “/previve” is:

[      script:previve] SCRIPT ERROR: @previve/server/previve-s.lua:53: attempt to call a nil value (global 'Vdist')
[      script:previve] > GetClosestPlayer (@previve/server/previve-s.lua:53)
[      script:previve] > ref (@previve/server/previve-s.lua:12)

Please help, I would really appreciate it.

Thank you :heart:

Hey :slight_smile:
In your GetClosestPlayer() function you have the following line :

local distance = Vdist(targetCoords.x,targetCoords.y,targetCoords.z,plyCoords.x,plyCoords.y,plyCoords.z)

It gives you an error because you try to call Vdist as a function, but she’s not declared and implemented anywhere as I can see.
You need to create you Vdist function or compare directly the coords in the GetClosestPlayer() function.
You can do either of those things with the following code (from the doc) :

local firstVec = vector3(0.0, 0.0, 0.0)
local secondVec = vector3(5.0, 5.0, 5.0)

local dist = #(firstVec - secondVec) -- Use Z
local dist = #(firstVec.xy - secondVec.xy) -- Do not use Z
1 Like

Does “local dist = #(firstVec - secondVec) – Use Z” calculate the distance between firstVec and secondVec?

Thanks a lot for your help! :slight_smile:

Yes it does, no probs !

1 Like