I'm assuming this is because you're not waiting for the player's Character to load.
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 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 string. 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 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!
If I did help, don't forget to accept this answer!
1 comment
change it into a local script