Infinite Yield Possible

This line is giving me an infinite yield error. I don’t know why this is happening because I’ve used this same line of code in another project and it didn’t give me any trouble. (It’s in a local script)

 local Cost = 100
local player = game:GetService("Players")


  local playerStats = player:WaitForChild("leaderstats")
  local playerGold = playerStats:WaitForChild("Gold")
  local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
  local Owned = gui.Shop.Stats.Frame.LongStick.Owned

script.Parent.MouseButton1Click:Connect(function()
  if playerGold.Value >= Cost and  Owned.Value == false then 
  playerGold.Value = playerGold.Value - Cost 
  Owned.Value = true

end
end)

19:01:28.838 - Infinite yield possible on ‘Players:WaitForChild(“leaderstats”)’

Can you give us the whole script? That should help us get why there is Infinite Yield.

Do you mean:

local playerStats = player.LocalPlayer:WaitForChild("leaderstats")

This would only work in a local script.

1 Like

I get it now. You’re trying to get the leaderstats from Players (the service), not the actual player.

Do what @Raretendoblox and @VegetationBush said instead.

local player = game.Players.LocalPlayer

player should be game.Players.LocalPlayer. You’re basically making the script wait until an instance with the name “leaderstats” will be created, which is never.

1 Like