Get Health Value

Hello,

So I was trying to do something simple like:


if ( ply:Health() < 50 ) then

and then do something.

But, for some strange reason, it just won’t work. And it’s not even giving me an error message either. I was wondering if anyone had a quick-fix for this? Anything I’m doing wrong?

try finding out what ply:Health() is equal to (like print(ply:Health()) or print(type(ply:Health())))

This is what I have so far:


function ThingSystem( ply )
	if ( ply:Health() < 50 ) then
		timer.Create("Thing", 1, 0, function()
			print( "Hi" )
		end)
	end

	if !ply:Alive() then
		timer.Destroy("Thing")
	end
end

But for some strange reason, it just won’t work. I’m not sure if it’s a timer problem, or the Health thing.

Did you call the function? - You could also hook it to a hook.

no i mean determine what ply:Health() is equal to, or just put a print(“hello world”) right below where the function is defined. if you don’t see anything being printed, like a number or hello world, then the function is not being called.
[editline]12/23/13[/editline]
fucking ninja’d

So I called it and I end up getting:


[ERROR]attempt to index local 'ply' (a nil value)

Are you calling the function without passing parameters? That’s what it sounds like. If ply is nil than you aren’t supplying an actual object in the function call.


function ThingSystem( ply )

and I’m calling it by, right under the whole code, doing:


ThingSystem( ply )

when you call ThingSystem(ply), stick a print(ply) in the line above that.

Is the ply object you’re passing valid? I think chances are, it isn’t. It’s essentially a NULL pointer, the object doesn’t exist, code isn’t magic, it needs to know where in memory to look.

Try something like this, to validate the object.

[lua]
local ply = nil;

for _, ob in pairs( player.GetAll() ) do
if( ob == LocalPlayer() ) then
ply = ob;

	break;
end


ply = false;

end

local function ThingSystem( ent_ply )
if( ent_ply:Health() < 50 ) then
timer.Create(“Thing”, 1, 0, function() print(“hi”) end)
elseif( !ent_ply:Alive() ) then
timer.Destroy(“Thing”);
end

return false;

end

if( ply ) then
ThingSytem( ply );
end
[/lua]

Well it ended up printing nil and then the error from the function being called.

[editline]24th December 2013[/editline]


[ERROR]attempt to call global 'LocalPlayer' (a nil value)

Going to try to find something else to put there.

what function is ThingSystem(ply) being called in? could you show us your code please?

What I showed is basically the whole code, I’m just calling it by simply saying: ThingSystem(ply) right under the function.

Please refer to my post, the issue is the ‘ply’ var doesn’t return a player object (LocalPlayer() function call would, though) so the object you’re passing through the function isn’t valid.

Right so, any way to validate it?

Look at my post, it’s all there. I showed you exactly how to create a valid Player object pointer.

Alright thank you so much! It worked, but…the timer won’t stop now.

That’s because the function is only being called once…if you want the timer to tell you when the players health is below 50%, you could do…

[lua]
– Declare empty ply var
local ply = nil;

– Locate Local player anchor, and copy pointer to our ‘ply’ variable
for _, ob in pairs( player.GetAll() ) do
if( ob == LocalPlayer() ) then
ply = ob;
end
end

– Think is called every frame, so if the players health every drops below 50 it will print ‘hi’, if you die it will print ‘dead’
hook.Add(“Think”, “CheckHealth”, function()
if( ply:Health() < 50 && !t ) then
print(“hi”);

	t = true;
elseif( !ply:Alive() && t ) then
	print("dead")


	t = false;
end

end)
[/lua]

I see what you tried to do, but I seem to be having an error right here:


[ERROR] ')' expected near '&'

[editline]24th December 2013[/editline]

Actually it works, the only problem is that it spams hi and dead when a player dies.

What exactly are you trying to make?