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

GUI that displays health?

Asked by
DanzLua 2879 Moderation Voter Community Moderator
10 years ago

I'm trying to change the text of a Textbox every 0.1 seconds but i keep ending up with this error. 19:21:44.656 - Players.Player.PlayerGui.ScreenGui.PS.TextBox.Script:3: attempt to index local 'plr' (a nil value) 19:21:44.657 - Stack Begin 19:21:44.657 - Script 'Players.Player.PlayerGui.ScreenGui.PS.TextBox.Script', Line 3 19:21:44.658 - Stack End

local name = script.Parent.Parent.Parent.Parent.Parent.Name
local plr = game.Workspace:FindFirstChild(name)
local health = plr:FindFirstChild("Humanoid").Health
local text = script.Parent.Text
while true do
    text = health
    wait(0.1)
end



Thanks

2 answers

Log in to vote
2
Answered by
Discern 1007 Moderation Voter
10 years ago

Just a few errors in assinging your variables. I fixed it for you:

--Locals took out, since they are not in the same scope in the loop.

name = script.Parent.Parent.Parent.Parent.Parent.Name --Just double check to make sure you have the right amount of "Parent"s.
plr = game.Workspace:WaitForChild(name) --WaitForChild is better in this situation.
humanoid = plr:FindFirstChild("Humanoid") --Health is in the humanoid, not the player. Also, you're setting the variable to the player's at that time, not the actual property itself.
text = script.Parent --Once again, you're setting the variable to the text of the GUI object, not the property itself.
while true do
    text.Text = humanoid.Health
    wait(0.1)
end
0
Thank you this helped a lot!!! DanzLua 2879 — 10y
Ad
Log in to vote
0
Answered by
KLGA 0
10 years ago

The only thing I see that could have been messed up is that you used a string value while searching for an object, now I may be wrong, but try this alternate code, be sure to use a LocalScript:

local plr = game.Players.LocalPlayer.Character
local health = plr:FindFirstChild("Humanoid").Health
local text = script.Parent.Text
while true do
    text = tostring(health) --It is a number value, we need this so it can be converted to text.
    wait(0.1)
end
0
Sorry it's not neat, I forgot to put it in a code block. KLGA 0 — 10y
0
Fixed it. KLGA 0 — 10y
0
It Didn't work @KLGA and there were no errors in the output:/ DanzLua 2879 — 10y

Answer this question