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.
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.
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.
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.
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.
% 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.
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.