Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

For Looping Events?

Asked by
Bman8765 270 Moderation Voter
9 years ago

I have a script that creates a bunch of custom image labels that could be located anywhere which is why it is difficult for me to just make an event for that item when I have no clue where it will be located. I say this because I'm making a custom Inventory GUI and when the user joins the game it organizes their "items" a.k.a imagelabels into slots in the inventory so it is difficult for me to just say imagelabel.OnEvent:connect when I have no clue what exact slot a.k.a frame it will be in.

This is why I made this function:

local items = returnItemsInInv()

It returns all the items in the user's inventory giving it to you in a table that can be used easily. (You can see the code below).

So then I use a for loop to go through all these items and if an event for one of them is ever called it will proceed in doing that event. My issue is the loop makes the events happen over and over again. whenever a user triggers the event it will do what is inside the event 10 times in a row before stopping (10 is an estimate, it is probably more).

I just need some other brains then my own to kinda look over my code and tell me how I can improve things and fix this current issue.

If you have any questions, just ask!

Here is the code:

This is for the function returnItemsInInv() :

function returnItemsInInv()
    local items = {}
    for _,slot in pairs (script.Parent.Inventory:GetChildren()) do
        if (#slot:getChildren() > 1) then
            for _,item in pairs (slot:getChildren()) do
                if (item.ClassName == "ImageLabel") then
                    local num = table.maxn(items)
                    table.insert(items, num, item)
                end
            end
        end
    end
    return items
end

This is the loop that is watching for events:

while true do
    local items = returnItemsInInv()
    for _,item in pairs (items) do
        item.DragStopped:connect(function(X, Y)
            print(("The player stopped dragging the GuiObject at (%i, %i)"):format(X, Y))
            checkCollision(item)
        end)
        item.DragBegin:connect(function(UD2)
            local XScale, XOffset, YScale, YOffset = UD2.X.Scale, UD2.X.Offset, UD2.Y.Scale, UD2.Y.Offset
            print(("The player started dragging the object at UDim2.new(%f, %d, %f, %d)"):format(XScale, XOffset, YScale, YOffset))
            local itemslot = item.Parent
            item.Parent = script.Parent.Inventory
            item.Size = UDim2.new(0.2,0,0.2,0)
        end)
    end
    wait()
end

Answer this question