I am trying to make a custom health system that displays names of players. I need this a lot in scripting so I'd like to get it out of the way. Please and thank you.
Player = game.Players.LocalPlayer script.Parent.Text = Player.Character.Humanoid.Health script.Parent.Parent.PlayerName.Text = Player.Name
In a server, things take a while to load. When making scripts, especially Local Scripts
, it's important to use WaitForChild
.
In your case however, we're going to use the Character Added Event
.
--Local Script, Probably in StarterGui/PlayerGui local Player = game.Players.LocalPlayer local Character = Player.Character or Player.CharacterAdded:wait() local Humanoid = Character:WaitForChild("Humanoid") script.Parent.Text = Humanoid.Health script.Parent.Parent:WaitForChild("PlayerName").Text = Player.Name
I also Waited for the Humanoid to load and PlayerName
to load.
Possible problems,
The Health Property of the Humanoid is a Int/Number Value
, not a strin
g. This shouldn't matter because rbx.Lua should make it into a string, but just in case I'm going to use the tostring
function
to make Humanoid.Health into a string
,
--Local Script, Probably in StarterGui/PlayerGui local Player = game.Players.LocalPlayer local Character = Player.Character or Player.CharacterAdded:wait() local Humanoid = Character:WaitForChild("Humanoid") script.Parent.Text = tostring(Humanoid.Health) script.Parent.Parent:WaitForChild("PlayerName").Text = Player.Name
That might make the script a little larger. If you wanted to shorten your script, you don't really have to use variable, but I would recommend it.
I hope I helped!
Good Luck!