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
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
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