Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago
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
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)' TheeDeathCaster 2368 — 9y

1 answer

Log in to vote
2
Answered by 9 years ago

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

0
How do you do them big letters? When you said 'LocalPlayer' in like <h1> letters.. Goulstem 8144 — 9y
0
? What do you mean? attackonkyojin 135 — 9y
0
Type in the word, hit enter, then put 5 'dashes', then hit enter again DigitalVeer 1473 — 9y
Ad

Answer this question