I have been fiddling with some entities in Lua on Garry's Mod, (User interaction seems to be very fun to me), and I would like to display the health of the entity that I have defined as self.damage on the server here:
function ENT:OnTakeDamage(dmg)
self.damage = (self.damage or 100) - dmg:GetDamage()
if self.damage <= 0 then
self:Remove()
end
end
end
I would now like to display what the entities "damage" (health) is at VIA a draw.SimpleText on the client. I am very new to Lua and this one just stumped me for some reason.
If I missed anything, please let me know.
Any ideas are greatly appreciated! Thanks!
we'll use Entity/SetNWInt to remember the health as well as send it to all clients
and Entity/GetNWInt to retrieve the health.
function ENT:OnTakeDamage(dmg)
local health=self:GetNWInt("health",100) - dmg:GetDamage()--
if health <= 0 then
self:Remove()
else
self:SetNWInt("health",health)
end
end
Okay, thank you very much for the input guys.
So, I'm using SetNWInt just as you put it.
I believe the issue I'm having is with retrieving the data in my cl_init file. Here are these:
init.lua
function ENT:OnTakeDamage(dmg)
local health=self:GetNWInt("health",100) - dmg:GetDamage()--
if health <= 0 then
self:Remove()
else
self:SetNWInt("health",health)
end
end
cl_init.lua
draw.SimpleText("Health:" ..Entity:GetNWInt(health),"DoorFont",-575,-525,Color(255,255,255,255),TEXT_ALIGN_LEFT,TEXT_ALIGN_TOP)
From my understanding, the issue is that I'm not sure how to call an Entity when I can't use self: or ENT:
You have Entity:GetNWInt(health) it should be self:GetNWInt("health")
function ENT:Draw()
draw.SimpleText("Health:" .. self:GetNWInt("health",100),"DoorFont",-575,-525,Color(255,255,255,255),TEXT_ALIGN_LEFT,TEXT_ALIGN_TOP)
end
just an example. you forgot to make the first arguement a string and you also forgot to give entity:GetNWInt a number for argument #2.
argument #2 is what is returned when the value has not been set, so if it's nil, then it'll return nil until set, this'll cause errors relating to concatenating nil values.
Ahh, that worked. Makes much more sense now. Thank you so much guys, huge help!
Sorry, you need to Log In to post a reply to this thread.