[英]Attempt to call global 'localPlayer' (a nil value) error in Garry's Mod
I need help with my code, when I run it I get the error 'attempt to call global 'localPlayer' (a nil value)' I want to make a hunger system in sandbox in my own hud.我的代码需要帮助,当我运行它时,我收到错误“尝试调用全局'localPlayer'(一个零值)”我想在我自己的 hud 中的沙箱中创建一个饥饿系统。 I found an addon with food there I took models and scripts and instead of getting hp, I need the variable with food to change, but there is a lot of food and a separate script is allocated for each food
我在那里找到了一个带有食物的插件,我使用了模型和脚本,而不是获取 hp,我需要更改带有食物的变量,但是有很多食物,并且为每种食物分配了一个单独的脚本
here my code这是我的代码
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )
function ENT:SpawnFunction( ply, tr )
if !tr.Hit then return end
local SpawnPos = tr.HitPos + tr.HitNormal * 1
local ent = ents.Create( "AppleJuice" )
ent:SetPos( SpawnPos )
ent:Spawn()
ent:Activate()
return ent
end
function ENT:Initialize()
self.Entity:SetModel("models/FoodNHouseholdItems/juicesmall.mdl")
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
self.Entity:SetSolid( SOLID_VPHYSICS )
self.Index = self.Entity:EntIndex()
local phys = self.Entity:GetPhysicsObject()
if phys:IsValid() then
phys:Wake()
end
end
function ENT:Use()
local ply = LocalPlayer()
--local currentHungerLevel = ply:GetNWInt('foodSostoyanie') or 100
--ply:SetNWInt('foodSostoyanie', math.Clamp(currentHungerLevel - 10, 0, 100))
--activator:SetHealth(activator:Health()-10)
--local foodSostoyaniee = require "clientloa.lua"
--foodSostoyaniee = foodSostoyanie
--local food = foodSostoyanie - 5
local currentHungerLevel = ply:GetNWInt('hunger_level') or 100
--ply:SetNWInt('hunger_level', math.Clamp(currentHungerLevel + 10, 0, 100))
self.Entity:Remove()
activator:EmitSound("eating_and_drinking/drinking.wav", 50, 100)
end
LocalPlayer() is a global function accessible only on client. LocalPlayer() 是一个全局 function 只能在客户端访问。 The ENT:Use hook is accessible only on server, so you are trying to access a clientside only function on a server.
ENT:Use 钩子只能在服务器上访问,因此您试图在服务器上仅访问客户端 function。 Think about it - local player does not make a sense on the server, it only makes sense on the client - it represents a player entity.
想一想——本地播放器在服务器上没有意义,它只在客户端上有意义——它代表一个播放器实体。 If you want to access the player that have used the entity - use a ENT:Use parameters:
如果您想访问已使用实体的玩家 - 使用 ENT:Use 参数:
function ENT:Use(activator, caller, useType, value)
//Heal the user
activator:SetHealth(activator:Health() + 10)
end
Refer to this page on garry's mod wiki: https://wiki.facepunch.com/gmod/ENTITY:Use请参阅 garry 的 mod wiki 上的此页面: https://wiki.facepunch.com/gmod/ENTITY:Use
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.