#how do I organize this for loop

1 messages · Page 1 of 1 (latest)

frosty anchor
#

im trying to order these frames so that the loop checks if the frame is empty and if it dosent skips over it, the problem is that it skips over some slots because it is unorganized
example:
outputVV

 2 (i) slot1(v)  -  Server - Script:13
 3 slot2  -  Server - Script:13
 4 slot3  -  Server - Script:13
 5 slot6  -  Server - Script:13

the codevvvvvvvvvv

local part=script.Parent

part.ProximityPrompt.Triggered:Connect(function(plr)
    local sg=plr.PlayerGui.backpack

    for i,v in sg:GetChildren() do
        if v:IsA("Frame") then
            print(i, v)
            if v.itemtype.Value=="0"then
                v.itemtype.Value=part.partype.Value
                v.itemamount.Value=part.partype.typeamount.Value
                sg.triggercheck:FireClient(plr)
                break
            elseif v.itemtype.Value=="wood" then
                v.itemamount.Value+=part.partype.typeamount.Value
                sg.triggercheck:FireClient(plr)
                break
            end
        end
    end
    
end)
abstract dove
#

replace this for i,v in sg:GetChildren() do by this for i,v in ipairs(sg:GetChildren() ) do

fossil hollow
#

vadasagma

#

watersigma

frosty anchor
#

btw i forgot to put this in the post

dusty salmon
#

roblox does a lot of orderering of data under the hood, it is convenient sometimes but other times particularly when you care about order you should be specific about the order.

#

aka table.sort where?

tribal timber
#
local part=script.Parent

local MaxSlots = 6
part.ProximityPrompt.Triggered:Connect(function(plr)
    local sg=plr.PlayerGui.backpack

    for i=1,MaxSlots do
        if sg:FindFirstChild("slot"..i) then
            local v = sg:FindFirstChild("slot"..i)
            if v.itemtype.Value=="0"then
                v.itemtype.Value=part.partype.Value
                v.itemamount.Value=part.partype.typeamount.Value
                sg.triggercheck:FireClient(plr)
                break
            elseif v.itemtype.Value=="wood" then
                v.itemamount.Value+=part.partype.typeamount.Value
                sg.triggercheck:FireClient(plr)
                break
            end
        end
    end

end)```
ripe kindle
#
local frames = sg:GetChildren()

table.sort(frames, function(a, b)
    return tonumber(a.Name:match("%d+")) < tonumber(b.Name:match("%d+"))
end)

Should look like this if you want it to be ordered and then when you iterate over it you use ipairs. but only if your frame is named "Slot1", "Slot2" etc

tribal timber
#

Or you can do #sg:GetChildren() and then it will still only react to Found Children "slot"..i

#

and just do a return on if it doesn't find "slot"..i and it will stop at max

#

infact I'll just do it

#
local part=script.Parent

part.ProximityPrompt.Triggered:Connect(function(plr)
    local sg=plr.PlayerGui.backpack

    for i=1,#sg:GetChildren() do
        if sg:FindFirstChild("slot"..i) then
            local v = sg:FindFirstChild("slot"..i)
            if v.itemtype.Value=="0"then
                v.itemtype.Value=part.partype.Value
                v.itemamount.Value=part.partype.typeamount.Value
                sg.triggercheck:FireClient(plr)
                break
            elseif v.itemtype.Value=="wood" then
                v.itemamount.Value+=part.partype.typeamount.Value
                sg.triggercheck:FireClient(plr)
                break
            end
        else
            return
        end
    end

end)```
#

That will return once it's reached the highest slot

frosty anchor
#

wouldn't it be easier if just make a new table and put the frames there or would it be less efficient

tribal timber
#

Why do that though?

frosty anchor
#

its easier to understand