game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local PlayerTag = script.Parent:WaitForChild("NameTag")
local healthbar = PlayerTag.Backing:WaitForChild("HealthBar")
local barhealth = healthbar:WaitForChild("BarBacking")
local bar = barhealth:WaitForChild("Bar")
local healthtext = barhealth:WaitForChild("HealthText")
local playerhealth = Player.Character.Humanoid.Health
local maxhealth = Player.Character.Humanoid.MaxHealth
local parent = script.Parent.Parent
local plrchar = Player:FindFirstChild("Character")
local humanoidchar = Player.Character.Humanoid
local humanoid = Player.Character.Humanoid
local plr = game.Players.LocalPlayer
local hum = Player.Character.Humanoid
local OldHealth = hum.Health
local plr = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
healthtext.Text = humanoid.Health .."/".. humanoid.MaxHealth
hum.HealthChanged:Connect(function()
local CurrentHealth = humanoid.Health
local MaxHealth = humanoid.MaxHealth
healthtext.Text = CurrentHealth .."/".. MaxHealth
bar.Size = UDim2.new(CurrentHealth / MaxHealth, 0, 1, 0)
repeat until nil
end)
end)
end)
This is my script, anyway I could fix it without using repeat until nil?
I don’t understand why you are even using a repeat in the first place, the Humanoid.HealthChanged will fire every time the health is changed, so there is no need to loop it.
So all you need to do to make it without the “repeat until nil” is simply removing the “repeat until nil”.
Also… why do you define SO MANY variables, many of them being exactly the same thing…
You define “humanoid” once and “hum” twice. And you also get the player from the PlayerAdded function, so why do you set the player using game.Players.LocalPlayer?
Not sure why it isn’t working, but it isn’t surprising because of all the random variables that you are using.
Please just use this code and build upon it to achieve what you want:
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local hum = Character:WaitForChild("Humanoid")
hum.HealthChanged:Connect(function()
local CurrentHealth = hum.Health
local MaxHealth = hum.MaxHealth
print(CurrentHealth .."/".. MaxHealth)
end)
end)
end)
This code will print the remaining health every time the health changes.
Also something important to note when testing: Make sure when you change the health of the humanoid, you change it on the server. Opening the explorer and changing the health of humanoid down from 100 will not work.
You can’t get the player from the server like that, you dont need this line.
Using PlayerAdded event [with a Player parameter] will give you the player.
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local PlayerTag = script.Parent:WaitForChild("NameTag")
local healthbar = PlayerTag.Backing:WaitForChild("HealthBar")
local barhealth = healthbar:WaitForChild("BarBacking")
local bar = barhealth:WaitForChild("Bar")
local healthtext = barhealth:WaitForChild("HealthText")
local playerhealth = Character.Humanoid.Health
local maxhealth = Character.Humanoid.MaxHealth
local parent = script.Parent.Parent
local hum =Character.Humanoid
local OldHealth = hum.Health
local char = Character or Player.CharacterAdded:Wait()
healthtext.Text = hum.Health .."/".. hum.MaxHealth
hum.HealthChanged:Connect(function()
local CurrentHealth = Character.Humanoid.Health
local MaxHealth = Character.Humanoid.MaxHealth
healthtext.Text = CurrentHealth .."/".. MaxHealth
bar.Size = UDim2.new(CurrentHealth / MaxHealth, 0, 1, 0)
end)
end)
end)
If you’re using a UI , and want it to update from server, this is totally fine.
[Alternatively you could use a remote from client to server - if you wanted the UI to be updated and seen for everyone].
so as i said idk whats the problem but yeah maybe its smth with HealthChanged cuz other code looks fine exept this repeat until nil out of nothing and a ton of useless variables