sesbaincoder8111
sesbaincoder8111

Reputation: 1

How do I fix Roblox Infinite Yield Error?

<---This is my current local script--->

local replicatedStorage = game:GetService("ReplicatedStorage")
local starterRebirthAmount = 5000

local player = game.Players.LocalPlayer
local mainFrame = script.Parent:WaitForChild("MainFrame")
local rebirthMenu = mainFrame:WaitForChild("RebirthMenu")
local mainButton = script.Parent:WaitForChild("MainButton")
local rebirthButton = rebirthMenu:WaitForChild("RebirthButton")
local strengthToRebirth = rebirthMenu:WaitForChild("StrengthToRebirth")
local rebirths = player:WaitForChild("leaderstats").Rebirths <--The Bit I think is causing the 
problems.

strengthToRebirth.Text = "You need at least "..math.floor((starterRebirthAmount + (rebirths.Value) * 
math.sqrt(500000))).." strength to rebirth"

mainButton.MouseButton1Click:Connect(function()

mainFrame.Visible =  not mainFrame.Visible

end)

rebirthButton.MouseButton1Click:Connect(function()
local result = replicatedStorage.Remotes.Rebirth:InvokeServer()
if result == true then
rebirthButton.Text  = "Successfully Rebirthed"
wait(1)
rebirthButton.Text = "CLICK HERE TO REBIRTH"
elseif result == "NotEnoughStrength" then 
rebirthButton.Text = "Not Strong Enough!"
wait(1)
rebirthButton.Text = "CLICK HERE TO REBIRTH"

end
end)

rebirths:GetPropertyChangedSignal("Value"):Connect(function()

strengthToRebirth.Text = "You need at least "..math.floor((starterRebirthAmount + (rebirths.Value) * 
math.sqrt(5000000))).." strength to rebirth" end)

I get the same error every time. 21:10:20.963 -

Infinite yield possible on 'Players.Archerofcool:WaitForChild("leaderstats")'

Upvotes: 0

Views: 2190

Answers (2)

Kylaaa
Kylaaa

Reputation: 7203

WaitForChild() is good if you have to wait for things to load into the world, like a player and their appearance. But as Piglet pointed out, not providing a timeout will cause it to throw errors like the one you're seeing.

When you provide a timeout, the function will return nil, and you need to be able to react to that case.

If you are sure that everything had loaded in by the time your script executes, you can simply grab everything like you would a normal index.

-- wait for the stuff to load...
while script.Parent:WaitForChild("MainFrame", 1) == nil do
    wait(1)
end

-- grab all the elements
local mainButton = script.Parent.MainButton
local mainFrame = script.Parent.MainFrame
local rebirthMenu = mainFrame.RebirthMenu
local rebirthButton =  rebirthMenu.RebirthButton
local strengthToRebirth = rebirthMenu.StrengthToRebirth

-- wait for the player to load in
local player = game.Players.LocalPlayer
while player:WaitForChild("leaderstats", 1) == nil do
    wait(1)
end
local rebirths = player.leaderstats.Rebirths

Upvotes: 2

Piglet
Piglet

Reputation: 28994

Please read the documentation befor you use any functions!

From https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild

WaitForChild

If a call to this function exceeds 5 seconds without returning, and no timeOut parameter has been specified, a warning will be printed to the output that the thread may yield indefinitely; this warning takes the form Infinite yield possible on 'X:WaitForChild("Y")', where X is the parent name and Y is the child object name.

Upvotes: 1

Related Questions