✓ Accepted answer
You're doing several things wrong.
First: you want to make sure the Character has loaded in before you assign it to a variable,
Second: You're indexing "StarterGui" instead of the player's PlayerGui.
Also, just as a tip, the minimum wait time for Lua is 1/60. There is no reason for you to try to update it faster or every 1/60th of a second. Please just use the HealthChanged event of the humanoid.
Here's an improved script:
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer;
local HealthBar = LocalPlayer.PlayerGui:WaitForChild("Bar"):WaitForChild("Frame"):WaitForChild("HP");
local Connection;
function newCharacter (character)
local Humanoid = character:FindFirstChildOfClass("Humanoid");
if Humanoid then
if Connection then Connection:disconnect() end
Connection = Humanoid.HealthChanged:Connect(function()
HealthBar = Humanoid.Health .. "%"
end)
else
warn("No humanoid found!");
end
end
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
newCharacter(Character);
LocalPlayer.CharacterAdded:Connect(newCharacter);
2 comments
put the script in starter character scripts
that isn't needed, local scripts can work anywhere on the client