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