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

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

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!

2 comments

0

your script is pretty confusing but your question and description explains better

0

I don't understand why you're using a local variable for script.Parent if you're literally doing script.Parent.Text?

3 answers

✓ Accepted answer

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)

1 comment

0

ty for the help!

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

3 comments

0

it runs all the time but it only updates if the health is < 100

0

Didn't work ;-;

0

This is pretty much the same script and does not fix the problem.

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)

3 comments

0

This answer is has errors to it.

0

Also, uses deprecated Instance:Changed

0

oh yeah, forgot to concatenate. Lemme try fix it

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