Infinite Yield + Am I Understanding My Code

This is my first time making a shop system. I watched an AlvinBlox tutorial, but changed a code a bit to fit my needs. I’m getting and infinite yield error in my ServerScript line 2. I also added comments to almost every line of code explaining what each line does. Could you please tell me if interpreted any line of code incorrectly. Thanks

ServerScript

local replicatedStorage = game:GetService("ReplicatedStorage")--Defines replicated storage
local shopItems = game:GetService("ServerStorage"):WaitForChild("ShopItems")--Defines the items in ServerStorage

replicatedStorage.CheckSale.OnServerInvoke = (function(player,item)--Responds to the remote function  request
	
	local price = shopItems[item].Points.Value--Defines the price of the item
	
	if player.leaderstats.Points.Value >= price then--Check to see if the player has enough money
		
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - price--Subtracts the price
		
		local gear = shopItems[item[item]]:Clone()--Clones the item they bought
		gear.Parent = player.Backpack--Put the clone in their backpack
		
		
		return true
	else
			return false
	
	end
end)

Local Script

local replicatedStorage = game:GetService("ReplicatedStorage")--Defines replicated storage
local selectedItem = script.Parent.Parent:WaitForChild(("SelectedItem"))--Defines what selectedItem is

local success--Variable

script.Parent.MouseButton1Click:Connect(function()
	
	success = false--Makes success false
	if selectedItem.Value ~= nil then--If the value isn't equal to nil
		success = replicatedStorage.CheckSale:InvokeServer(selectedItem.Value)--Make a request to the remote function
	end
	
	if success then
		print("Purchased!")
		
	else
			
			print("Purchase Failed!")
	end
	
end)

Infinite yield usually means you can’t find the names instance
Make sure that something named “ShopItems” is in server storage, also I usually use FindFirstChild for server scripts as everything should be loaded on the server

Also it could be helpful if you post an image of everything inside serverstorage for that matter

because the thread could yield infinitely , as :WaitForChild() didn’t return an instance within 5 seconds; make sure what you’re indexing is the right name , you can also try doing

:WaitForChild("ShopItems", 8) -- breaks and returns nil if instance isn't found

It looks like this should work. If you are building in a group place, have team create on, or have drafts on, maybe it could be an issue of not committing the draft

  • 1 Your code looks fine

  • 2 Infinite yield usually means that :WaitForChild("") couldn’t find the child that you specified.

Anyway, I noticed that in your Script (Not localscript, script) at line 2, you put this line of code:

local shopItems = game:GetService("ServerStorage"):WaitForChild("ShopItems")--Defines the items in ServerStorage

But in the image you have shown us, ShopItems is exactly the same as the ShopItems string that you put into the :WaitForChild("") function.

After doing a few tests with line 2 of your script, I have noticed putting a space after the folders name, an issue like this might happen. (Where your folder in server-storage is the same name as the folder in :WaitForChild(""))

Make sure that the name of your folder in server storage doesn’t have a space after it’s name.

Tell me if that didn’t fix it though.

The best way to debug the ‘Infinite Yield’ message is to do one of three things

  • Verify the instance was found in your code by printing out a message afterwards
local Instance = ServerStorage:WaitForChild("ShopItems")
print("Found " .. Instance.Name)
  • Check your parent (the instance the item is in) to make sure the item is actually there
  • (if you want to remove it) add a predefined timeout value to WaitForChild()
local Instance = ServerStorage:WaitForChild("ShopItems", 30)

Many of Roblox’s built-ins tend to print this warning as well, as long as you can verify the folders exist and have been found, you can simply dismiss this warning