Confusing Error please help!

I keep getting this error and I don’t understand why >:(

[ERROR] lua/autorun/test.lua:5: attempt to call method ‘Health’ (a nil value)

  1. unknown - lua/autorun/test.lua:5

local user = player.GetAll()

local function healthregen()

if ( user:Health() < 100 ) then
		
		user:SetHealth(1)

else

	if ( user:Health() > 100 ) then return end
	
	end
end

timer.Create("playerhealthregen", 0, 0, healthregen)

“user” is a table, not a player.

I tried to make the function “ply” but it kept giving me errors, so “user” removed those errors for me

If you tell us what you’re trying to do then we could help you better and

player.GetAll returns a table, not a player object.

well I am trying to create a health regeneration script, and what do I make my ply variable because, “LocalPlayer()” is client side so what is the server side variant of it?


function Regenerate(ply) -- Move hooks have 2 arguments: a player object, and CMoveData.
    if (ply.NextRegen > curtime) then return end -- if player has already regenerated then don't regen more health
    ply.NextRegen = curtime + regen time -- set player's next regen time in seconds.
    
    if (ply's health is smaller than ply's max health) then
        local current health = ply's health
        set ply's health to clamp(current health + added health, 0, ply's max health)
    end
end
create "Move" hook with Regenerate


CurTime

Entity:Health

Entity:GetMaxHealth

Entity:SetHealth

math.Clamp

hook.Add

GM:Move

“ply’s health is smaller than ply’s max health” didn’t realise that was code?

It isn’t. Use some common sense to make it work.

cheers!

[editline]1st October 2017[/editline]

you mind helping me with this error?


local function HealthCheck( ply )

	for k, v in pairs(ply) do
		
		v:Health() < v:GetMaxHealth() then
			
		timer.Create("HPRegen", 1, 0, function()
			
			v:SetHealth(1)

			end)

		end

	end

end

Things wrong there:

  1. Assuming that ‘ply’ is a player, why would you iterate through it? (for k, v…)
  2. You didn’t put the ‘if’ statement before ‘v:Health()’
  3. Timers must have unique names, if you call that function for each player, the game will not create a separate timer for each of them, because the name would be ‘HPRegen’ for all of them.
  4. :SetHealth(1) means you’re setting their health to 1, not adding 1 to their current health.
  5. The timer would run forever, even after the player died (I’m not entirely sure if that would cause any kind of errors or unexpected consequenses, but still).

Instead of reinventing the wheel why not download this off workshop and just look at the code and learn from it?

So do you mean like this?


local function HealthRegen( ply )

	for k, v in pairs(player.GetAll()) do
		
		if v:Health() < v:GetMaxHealth() then

			timer.Create("playerHpRegen", 1, 10, function()
				
				v:Health() + 1

			end)

		end


	end

[editline]2nd October 2017[/editline]

thank you, I will look at it now! :slight_smile:

You’re doing:

[lua]var + 1[/lua]

You have to do

[lua]var = var + 1[/lua]

Health is the Accessor, you have to use SetHealth()

so would you do at the top of the file


regen = player.SetHealth() + 1

Can you stop and think what SetHealth() would do?
Use the wiki if you don’t know what the functions do

Entity:SetHealth

Also you’re doing player.Method, that’s not how entities methods works, in lua you do player:Method() since you’re calling a method of an object, not of a table

ohh yea ok, that was a typo sorry, thanks for your help man