Script doesn't work for some reason

I’m pretty sure this functions only ONCE since the Health Bar UI is displaying the health and maxhealth is 100.

local Game = game; local Humanoid = script.Parent:WaitForChild("Humanoid", 2); local RS = game:GetService"RunService";

local BG = script.Parent.BillboardGui.Frame

--Humanoid.HealthChanged:Connect(function(Health)
	
	--local H = Health
	--local MH = Humanoid.MaxHealth;
	
	--BG.Frame.Size = UDim2.new((H / MH), 0, 1, 0)
	
	--BG.TextLabel.Text = H.."/"..MH
	
--end)

while wait() do
	
	local H = Humanoid.Health
	local MH = Humanoid.MaxHealth;

	BG.Frame.Size = UDim2.new((H / MH), 0, 1, 0)

	BG.TextLabel.Text = H.."/"..MH
	
	if Humanoid.Health < 1 then
		
		game.Debris:AddItem(script.Parent, 0)
		
	end
end


This script is contained in a Model, which the PLAYER can destroy.

You need a check for whether the model still exists within your while wait() loop or it will continue running forever even after the model is destroyed.

I presume you are saying the model is still there in the workspace. It’s, I have a simple tool that would damage anything, even the model’s humanoid. But, never constantly displays IT’s health. I may add the model is still in the workspace even though the Humanoid’s health is below 0.

Is this running in a script or localscript? It’s possible that this is running in a server script but your damaging tool is using a localscript. In that case, the damage wouldn’t replicate to the server unless you use remote events.

1 Like

Yeah you are right, I was very stressed about it :sweat_smile:

Idk whats ur problem here, but try if this does the job

Oh and this needs to be fixed also

@anmocha

The ideal solution would be the Humanoid.HealthChanged event as opposed to an infinite loop. Now that you know the issue isn’t your event, you should switch back to using HealthChanged.

i didnt know such a thing as HealthChanged existed :open_mouth: