[英]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.我目前正在编写一个特殊的复活脚本,允许普通玩家在没有 EMS 的情况下互相复活。
This is the code I have in my "previve-s.lua" (server-sided script):这是我在“previve-s.lua”(服务器端脚本)中的代码:
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":这是我在“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:当我尝试“/previve”时,我从控制台得到的错误是:
[ 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.我不是 FiveM 方面的专家,但根据文档,服务器 API 中没有Vdist
函数。 That's a function in the client API这是客户端 API 中的一个函数
https://docs.fivem.net/natives/?_0x2A488C176D52CCA5 https://docs.fivem.net/natives/?_0x2A488C176D52CCA5
In line 53 you're trying to call a nil value which should be Vdist.在第 53 行中,您尝试调用应为 Vdist 的 nil 值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.