Is There Anything Wrong With This Script

I Just Want To Know Why The Script Has A Warning That Says
Infinite yield possible on ‘Players.CollapsedDominos.PlayerGui.ScreenGui.Frame.HealthBar:WaitForChild(“HealthBar”)’
This Is The Health Bar Script

-- Disable the healthbar
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health,false)

--Variables
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local healthbar = script.Parent:WaitForChild("HealthBar")

-- Updating the healthbar
game:GetService("RunService").RenderStepped:Connect(function()
	local humanoid = char:FindFirstChild("Humanoid")
	healthbar.Bar.Size = UDim2.new(humanoid.Health/humanoid.MaxHealth,0,1,0)
	healthbar.HealthStatus.Text = math.floor(humanoid.Health).."/"..humanoid.MaxHealth
end)

Please Let Me Know

3 Likes

Its a warning. So it doesnt do anything to your script.

If you wonder why it does give this warning : Its because you used Instance:WaitForChild() and it makes script says that when it thinks this child never gonna appear.

:smile:

edit = Wait a second in warning it says you tried to find HealtBar inside HealtBar. Are you sure thats right?

infinite yield possible means it can’t find the thing you’re trying to index, are you sure you’re indexing it right?

HealthBar is already referenced via script.Parent, the warning means that this instance doesn’t contain a child instance of the same name.

1 Like

It got infinity yield, that means the HealthBar turns into like nil now because it doesnt exist on location because it keeps finding where the HealthBar does.

So you must change code above to this V

local healtbar = script.Parent

The warning ‘infinite yield possible’ always means the script waited for the instance, but found nothing and timed out.

To ensure script functionality, you wanna verify that “HealthBar” is either located under or moved into the script’s parent. If the instance “HealthBar” is not found under the script’s parent within a certain amount of time, the function will time-out and skip to the next line.

If “HealthBar” is already currently under the script’s parent before even running the code, make sure the instances name matches the the name in the script. If it’s being moved into the script’s parent as the code runs, make sure to either give the script more time to run the function, or parent it sooner. Here’s how you can allow the script to run the function for your desired maximum time:

local healthbar = script .Parent:WaitForChild("HealthBar",10)

The second parameter (10) will change the amount of time it takes for the function to time out. Change this parameter to your desired amount of time in seconds if you wish to fix the issue this way. I really do suggest you parent the instance sooner though, as it decreases the amount of time it takes for the script to finish running. :wink:

1 Like

this is cause “HealthBar” doesn’t exist