Hello, ive made stats which include a health stat and a magic stat however the stats don’t load the actual players stats because I get the error “Infinite yield possible” here’s the code and I was wondering if there’s a fix for this? The MaxMagic and Magic is a number value in the characters Humanoid and I’ve done the same for Health however not a number value as the humanoid already has the health and maxhealth value inside it. It sometimes loads however sometimes doesn’t.
local tweenService= game:GetService("TweenService")
local progress = script.Parent
local MaxMagic = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):WaitForChild("MaxMagic")
local Magic = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):WaitForChild("Magic")
local left = progress:WaitForChild("Left"):WaitForChild("Circle")
local right = progress:WaitForChild("Right"):WaitForChild("Circle")
local center = progress:WaitForChild("Centre")
local percentage = Instance.new("NumberValue")
percentage.Parent = script.Parent
percentage.Name = "percentage"
local Text = script.Parent.TextFrame.TextLabel
percentage.Value = Magic
local rotation = math.floor(math.clamp(percentage.Value / MaxMagic.Value * 360, 0, 360))
right.UIGradient.Rotation = math.clamp(rotation, 0, 180)
left.UIGradient.Rotation = math.clamp(rotation, 180, 360)
Text.Text = Magic.Value.." / "..MaxMagic.Value
percentage:GetPropertyChangedSignal("Value"):Connect(function()
local rotation = math.floor(math.clamp(percentage.Value / MaxMagic.Value * 360, 0, 360))
Text.Text = Magic.Value.." / "..MaxMagic.Value
right.UIGradient.Rotation = math.clamp(rotation, 0, 180)
left.UIGradient.Rotation = math.clamp(rotation, 180, 360)
end)
Magic.Changed:Connect(function()
percentage.Value = Magic.Value
end)
Infinite Yield Possible means that one of the :WaitForChild() lines at the top is yielding indefinitely because it is not able to find the object referenced.
-- Function to simulate a task that takes time
local function simulateTimeConsumingTask()
for i = 1, 5 do
print("Processing step " .. i)
wait(1) -- Yielding for 1 second (you can adjust the time as needed)
end
end
-- Coroutine function to run the task
local function runTaskCoroutine()
print("Task started")
simulateTimeConsumingTask()
print("Task completed")
end
-- Connect the coroutine function to a button click
local button = script.Parent
button.MouseButton1Click:Connect(function()
coroutine.wrap(runTaskCoroutine)() -- Start the coroutine
end)
Looks like it’s unable to find the “Magic” and “MaxMagic” objects within the Humanoid. Ensure that those objects do exist locally when running the game.
Are you using a script to create those number values or are you creating them yourself when play-testing? If you’re creating them yourself then the problem you’re experiencing is because the script is running before you’ve created them and that warning shows up after 5 seconds have passed since the script first attempted to retrieve the value, so if you take longer than 5 seconds to create them it would cause the warning to appear