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

My Health System Doesn't work online?

Asked by 7 years ago

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
0
change it into a local script Warfaresh0t 414 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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

If I did help, don't forget to accept this answer!
Ad

Answer this question