Infinite yield on a player added event

I have a data store for a list of slots in an item storage gui. I want to load the images of the items stored on each slot when a player joins the game, but for some reason, it kept giving me an “Infinite yield possible” error

local serverstorage = game:GetService("ServerStorage")
local replicatedstorage = game:GetService("ReplicatedStorage")

local rownumber

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "Bank_Storage"
	folder.Parent = player
	
	for i = 1, 20, 1 do
		local Slot = Instance.new("StringValue")
		Slot.Name = "Slot".. i
		Slot.Parent = player.Bank_Storage
	end

	wait(0.5)

	for i = 1, 20, 1 do
		if i <= 5 then
			rownumber = 1
		elseif i > 5 and i <= 10 then
			rownumber = 2
		elseif i > 10 and i <= 15 then
			rownumber = 3
		elseif i > 15 and i <= 20 then
			rownumber = 4
		end

		for _, v in pairs(serverstorage.Items:GetChildren()) do
			if player.Bank_Storage:WaitForChild("Slot".. i).Value == v.Name then
				player.PlayerGui:WaitForChild("UI").StorageUI.Storage:WaitForChild("Row".. rownumber):WaitForChild("Slot".. i).Image = v.TextureId
				player.PlayerGui:WaitForChild("UI").StorageUI.Storage:WaitForChild("Row".. rownumber):WaitForChild("Slot".. i).ImageColor3 = Color3.new(255, 255, 255)
			end
		end
	end
end)

Please let me know if you want any other information.

This is where the error occurs if you are wondering.

Have the client request the image data and allow them to deal with it there; I’m saying this because the client is probably making all of the slots and rows which aren’t visible to the server.

try doing:

:WaitForChild("Slot".. i, 10) -- Checks for 10 seconds before timing out

The script is a server script and its in serverscriptservice as show in the picture.

I think I found what’s going wrong with your code.

In your row logic, the UI allows for 5 slots per row.
You can see that the console says it waits for Row 2, Slot 6 when there are only supposed to be 5 maximum slots per row.

If I had to guess, there needs to be some fixing up in this part of your code:

for i = 1, 20, 1 do
		if i <= 5 then
			rownumber = 1
		elseif i > 5 and i <= 10 then
			rownumber = 2
		elseif i > 10 and i <= 15 then
			rownumber = 3
		elseif i > 15 and i <= 20 then
			rownumber = 4
		end

it probably has something to do with the operators (<, >, <=, >=) you used in that code block.

1 Like

image
got this for some reason

Yes, and I am saying that you are most likely making the slots on the client. If I am correct, the server doesn’t see those slots, meaning the server will have an infinite wait. So, again, you should probably have the client request the slot data, and have it handle the slots there.

There aren’t any issues with his logic, though. If there are five slots per row, the sixth slot would be in the second row.

are you sure that this “Slot” is a ImageLabel, ImageButton, or Decal?

Yes, the slots are ImageButtons

That would be why.

player.PlayerGui:WaitForChild("UI").StorageUI.Storage:WaitForChild("Row".. rownumber):WaitForChild("Slot".. i%5 > 0 and i%5 or 5).Image = v.TextureId
player.PlayerGui:WaitForChild("UI").StorageUI.Storage:WaitForChild("Row".. rownumber):WaitForChild("Slot".. i%5 > 0 and i%5 or 5).ImageColor3 = Color3.new(255, 255, 255)

For the explanation, we need the remainder of i/5 so we can find the correct slot.

1 Like

The serverscript makes the slots, though thx for the suggestion, ill see if i can use fireclient.

Yes, I would still recommend changing the images on the client, but I have provided a fix to the problem above.

Oh ok. If thats the case, I would suggest making the rows and slots on a LocalScript and then replicating it to the server.

No need to replicate it to the server, it was already there. Even if it wasn’t, the server should never have to handle UIs, so it would be better to keep it on the client.

I am getting this error, by the way could you explain what % do? I am new to luau.


% gives you the remained of the number/another number (ex: 10%6 = 4). As for the compare number to a string, add parentheses around the statement.

player.PlayerGui:WaitForChild("UI").StorageUI.Storage:WaitForChild("Row".. rownumber):WaitForChild("Slot"..(i%5 > 0 and i%5 or 5)).Image = v.TextureId
player.PlayerGui:WaitForChild("UI").StorageUI.Storage:WaitForChild("Row".. rownumber):WaitForChild("Slot"..(i%5 > 0 and i%5 or 5)).ImageColor3 = Color3.new(255, 255, 255)

As another explanation, it was adding i%5 to the string and then comparing it to 0, so we needed the parentheses to prevent it from adding to the string.

1 Like

Thanks the script finally worked!

1 Like

I found out what was wrong as soon as I saw the picture of the slots and image buttons. Its looking for a Image button in row 2 called “Slot6” but they are only name “Slot(1-5)” instead of having 6, you might have to rename all of those image buttons in each row and dont make them start at one but instead start at what it should be in that row.

If you are just trying to sort the buttons then I suggest just using UIListLayout becuase it’s really helpful for this case.

1 Like