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
[CODE]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)[/CODE]
"user" is a table, not a player.
[QUOTE=txike;52734791]"user" is a table, not a player.[/QUOTE]
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 [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/player/GetAll"]player.GetAll[/URL] returns a table, not a player object.
[QUOTE=txike;52734804]If you tell us what you're trying to do then we could help you better and [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/player/GetAll"]player.GetAll[/URL] returns a table, not a player object.[/QUOTE]
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?
[code]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
[/code]
[IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Global/CurTime"]CurTime[/URL]
[IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Entity/Health"]Entity:Health[/URL]
[IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Entity/GetMaxHealth"]Entity:GetMaxHealth[/URL]
[IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Entity/SetHealth"]Entity:SetHealth[/URL]
[IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/math/Clamp"]math.Clamp[/URL]
[IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/hook/Add"]hook.Add[/URL]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/Move]GM:Move[/url]
[QUOTE=txike;52734827][code]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
[/code]
[IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Global/CurTime"]CurTime[/URL]
[IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Entity/Health"]Entity:Health[/URL]
[IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Entity/GetMaxHealth"]Entity:GetMaxHealth[/URL]
[IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Entity/SetHealth"]Entity:SetHealth[/URL]
[IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/math/Clamp"]math.Clamp[/URL]
[IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/hook/Add"]hook.Add[/URL][/QUOTE]
"ply's health is smaller than ply's max health" didn't realise that was code?
[QUOTE=PoseidonRias;52735004]"ply's health is smaller than ply's max health" didn't realise that was code?[/QUOTE]
It isn't. Use some common sense to make it work.
[QUOTE=txike;52735140]It isn't. Use some common sense to make it work.[/QUOTE]
cheers!
[editline]1st October 2017[/editline]
you mind helping me with this error?
[QUOTE]lua/autorun/test.lua:5: unexpected symbol near '<'
1. unknown - lua/autorun/test.lua:0[/QUOTE]
[CODE]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[/CODE]
Instead of reinventing the wheel why not download this off workshop and just look at the code and learn from it?
[url]https://steamcommunity.com/sharedfiles/filedetails/?id=183701075[/url]
[QUOTE=Ia_grib;52736050]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 [b]not[/b] 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).[/QUOTE]
So do you mean like this?
[CODE]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[/CODE]
[editline]2nd October 2017[/editline]
[QUOTE=TheVac;52736310]Instead of reinventing the wheel why not download this off workshop and just look at the code and learn from it?
[url]https://steamcommunity.com/sharedfiles/filedetails/?id=183701075[/url][/QUOTE]
thank you, I will look at it now! :)
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()
[QUOTE=gonzalolog;52737050]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()[/QUOTE]
so would you do at the top of the file [CODE]regen = player.SetHealth() + 1[/CODE]
Can you stop and think what SetHealth() would do?
Use the wiki if you don't know what the functions do [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/SetHealth]Entity:SetHealth[/url]
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
[QUOTE=gonzalolog;52737150]Can you stop and think what SetHealth() would do?
Use the wiki if you don't know what the functions do [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/SetHealth]Entity:SetHealth[/url]
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[/QUOTE]
ohh yea ok, that was a typo sorry, thanks for your help man
Sorry, you need to Log In to post a reply to this thread.