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

Why won't my health display work?

Asked by 7 years ago
for i, player in pairs(game.Players:GetChildren()) do
    if player:FindFirstChild("PlayerGui") then
        local plr = player
        for i, object in pairs(game.Workspace:GetChildren()) do
            if object:FindFirstChild("Humanoid") then
                local humanoid = object:FindFirstChild("Humanoid")
                if humanoid.Parent.Name == plr.Name then
                    local healthStatus = plr.PlayerGui.healthGui.healthFrame.healthStatus
                    healthStatus.Text = humanoid.Health.." / "..humanoid.MaxHealth
                end
            end
        end
    end
end

So after lots of different attempts with different approaches, I really thought this one would work but (clearly) it did not, but I can't figure out why?

(Goal of this script is to change the text of healthStatus to whatever the players health is out of their max health)

0
You're code is only running once, unless there is code you haven't included. GoldenPhysics 474 — 7y
0
Is it a script or a localscript? GoldenPhysics 474 — 7y
0
It was just a normal script Squidier 40 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago

This should work if you intend this to be on the server, given you don't have FE (FilteringEnabled) on. You didn't connect your code to run whenever a new player was added or when the health changed. You're script only ran once as soon as it loaded and never executed the code again.

function connectChanges(plr)
    plr.CharacterAdded:connect(function()
        local humanoid = plr.Character:FindFirstChild("Humanoid")
        if humanoid then

            local healthStatus = plr.PlayerGui:FindFirstChild("healthStatus", true) --true makes it recursive. Check the wiki for more
            humanoid.changed:connect(function()
                healthStatus.Text = humanoid.Health .. " / " .. humanoid.MaxHealth
            end)
        end
    end)
end

for i, plr in pairs(game.Players:GetChildren()) do
    connectChanges(plr)
end

game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterAdded:connect(function()
        connectChanges(plr)
    end)
end)

If you intended it to be on the client as a localscript (probably the better option) You need a variation on the last bit of code.

local plr = game.Players.LocalPlayer

plr.CharacterAdded:connect(function()
    local humanoid = plr.Character:WaitForChild("Humanoid")
    local healthStatus = plr.PlayerGui:FindFirstChild("healthStatus", true) --true makes it recursive. Check the wiki for more
    humanoid.changed:connect(function() --this is the vital part missing. Without it, the gui will never update.
        healthStatus.Text = humanoid.Health .. " / " .. humanoid.MaxHealth
    end)
end)

As you can see, I took out the parts connect the function in the event of players being added because local scripts will reload and execute whenever the character spawns. (If I'm wrong, use the other code without the loop going through all players)

0
Thank you so much :D Squidier 40 — 7y
1
I didn't actually test it, so tell me if it works. GoldenPhysics 474 — 7y
0
Yep, it does! One last question tho, is there a way to make it so it doesn't show all the decimal points while regenerating health? (or just if you can turn off health regen in general) Squidier 40 — 7y
1
On line 7 of my second script use math.floor(humanoid.Health) to truncate (get rid of) the decimal. GoldenPhysics 474 — 7y
Ad

Answer this question