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

How can I update text based on player's health, no errors?

Asked by 4 years ago
Edited 4 years ago

Trying to update text in the label (script's parent) based on health, it makes the text the player's health at first but doesn't update, I tried making a loop but.... yeah, nothing.

local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local noid = character:WaitForChild("Humanoid", 10)
local health = noid.Health
local label = script.Parent


script.Parent.Text = ""..health..""

while noid.Health < 100 do
    wait(.1)
    if noid.Health < 100 then
        label.Text = ""..health..""
    end
end

I'd really love to receive educational answers! Ty!

0
your script is pretty confusing but your question and description explains better Gameplayer365247v2 1055 — 4y
0
I don't understand why you're using a local variable for script.Parent if you're literally doing script.Parent.Text? DeceptiveCaster 3761 — 4y

3 answers

Log in to vote
2
Answered by
OnaKat 444 Moderation Voter
4 years ago
Edited 4 years ago

That because you not update health variable! you can do 2 ways

1.) Update "health" variable

2.) Use Text = character.Humanoid.Health

And if you want to make loop, do this..

while true do
    --script...
wait()
end

Here is example

local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
repeat wait() until character:FindFirstChild("Humanoid")
local label = script.Parent


script.Parent.Text = ""..character.Humanoid.Health..""

while true do
    label.Text = character.Humanoid.Health
wait()
end

But the best way is GetPropertyChangedSignal("Property Name")

Here is example

local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
repeat wait() until character:FindFirstChild("Humanoid")
local label = script.Parent


script.Parent.Text = ""..character.Humanoid.Health..""

character.Humanoid.Health:GetPropertyChangedSignal("Health"):Connect(function()
    label.Text = character.Humanoid.Health
end)
0
ty for the help! Gomenasa1 32 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local noid = character:WaitForChild("Humanoid")
local health = noid.Health
local label = script.Parent


script.Parent.Text = ""..health..""

while wait(.1) do
    if noid.Health < 100 then
        label.Text = ""..health..""
    end
end

0
it runs all the time but it only updates if the health is < 100 Gameplayer365247v2 1055 — 4y
0
Didn't work ;-; Gomenasa1 32 — 4y
0
This is pretty much the same script and does not fix the problem. namespace25 594 — 4y
Log in to vote
0
Answered by
Despayr 505 Moderation Voter
4 years ago
Edited 4 years ago

Using a while true do loop is not needed. Try updating it only when there is a change. This can be done using the changed function.

Second of all, you set the variable 'health' to the humanoids health value. Therefore, it will always be the same number

local plr = game:GetService("Players").LocalPlayer
local Character = plr.Character or plr.CharacterAdded:wait()
local noid = Character:FindFirstChildOfClass("Humanoid")
local label = script.Parent

script.Parent.Text = ""..noid.Health

noid.Changed:Connect(function()

    if noid.Health < 100 then
        label.Text = ""..noid.Health
    else
        label.Text = "100"
    end

end)
0
This answer is has errors to it. namespace25 594 — 4y
0
Also, uses deprecated Instance:Changed namespace25 594 — 4y
0
oh yeah, forgot to concatenate. Lemme try fix it Despayr 505 — 4y

Answer this question