Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't My player's stat rise when health gets below a certain number?

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

1 comment

0

'Health' is ALREADY a Property of 'Humanoid', you are attempting to call 'Value' on Property 'Health', thus an error that looks something like this: 'Attempt to call unknown value 'Value' on 'Health' (A number Value)'

1 answer

✓ Accepted answer

Health

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


LocalPlayer

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")

Events

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)

Useful Links

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

3 comments

0

How do you do them big letters? When you said 'LocalPlayer' in like

letters..

0

? What do you mean?

0

Type in the word, hit enter, then put 5 'dashes', then hit enter again

This discussion is preserved as it was. New answers, comments, and votes are closed.