简体   繁体   中英

Attempt to call a nil value LUA FiveM Coding

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 me, I would really appreciate it.

Thank you

I'm no expert in FiveM but according to the docs there is no Vdist function in the server API. That's a function in the client API

https://docs.fivem.net/natives/?_0x2A488C176D52CCA5

In line 53 you're trying to call a nil value which should be Vdist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM