Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

LocalScript doesn't work with Roblox Client?

So I'm setting a NumberValue as being the value of the players health. the following code is what I've done to do this:

local Player = game.Players.LocalPlayer
local Char = Player.Character
local Health = Char.Humanoid.Health

while true do
	script.Parent.Value = Health
	wait()
end

It works in studio, but for some reason, it doesn't work when using the roblox client. It just comes up saying that Char is a nil value. This is a local script in a custom health bar GUI, with the parent being a numbervalue. Can anyone help me out?

1 answer

✓ Accepted answer

You can add CharacterAdded event, and then HealthChanged event to humanoid. Then you don't need to worry about character.

local Player = game:GetService("Players").LocalPlayer
local Value = script.Parent

Player.CharacterAdded:connect(function(char)
	local Humanoid = char:WaitForChild("Humanoid")
	Value.Value = Humanoid.Health
	Humanoid.HealthChanged:connect(function()
		Value.Value = Humanoid.Health
	end)
end)

And don't forget to make it LocalScript!

1 comment

0

Thanks for that. There was a slight bug with the first line. I added a new variable called 'Players' and set it as game:GetService("Players") and edited 'Player' to be 'Players.LocalPlayer'.

This discussion is preserved as it was. New answers, comments, and votes are closed.