local hmn = script.Parent.Parent.Character.Humanoid local def = script.Parent.Parent.leaderstats.Defense while true do wait() if hmn.Health.Value <= (hmn.Health.Value / 100 * 20) then def.Value = (def.Value + 1) end end
Health
is a property itself, meaning you don't have to say .Value
because otherwise that causes an error.
For example, if you were to reference the Color of a part, you'd write:
color = part.BrickColor
And NOT:
color = part.BrickColor.Value
So rather, you should do:
hmn.Health not hmn.Health.Value
Rather than trying to reference the player through a series of parents in a complicated hierarchy, you should try and use LocalPlayer.
WARNING: The script needs to be a localscript for the LocalPlayer reference to work.
Your whole Player reference can be replaced with:
local plr = game.Players.LocalPlayer
A lot cleaner, huh?
Now, it becomes even easier to reference the Character and humanoid
local plr = game.Players.LocalPlayer local chr = plr.Character or plr.CharacterAdded:wait() local hum = chr:FindFirstChild("Humanoid")
An event is a specific scope that is 'fired' when a certain action occurs. For example, code inside the scope of a Touched event will run if a part is obviously Touched. So rather than using a while loop, you can keep an event to track when the humanoids health changes.
local plr = game.Players.LocalPlayer local chr = plr.Character or plr.CharacterAdded:wait() local hum = chr:FindFirstChild("Humanoid") local def = plr["leaderstats"].Defense hum.HealthChanged:connect(function(health) if health <= (hum.MaxHealth / 100 * 20) then def.Value = def.Value + 1; end end)
http://wiki.roblox.com/index.php?title=API:Class/Humanoid/HealthChanged http://wiki.roblox.com/index.php?title=API:Class/Humanoid http://wiki.roblox.com/index.php?title=API:Class/Players/LocalPlayer