Function activate self problem?

How can I make a function activate himself, here’s a bit more explenation about my problem. Let’s say I have a for loop wich needs to be activated when something is bought, but in the for loop we do mousebutton1up and then you but it so the bought function need to reactivate himself so the for loop will check all the new items. Here’s an example:

function loop()
     for i,v in pairs(something)
     item.mousebutton1up:connect(function()
     -- buy item
    -- and here inseard new item but then we need to reactivate the function
     loop()
    end
end)

is this possible? – I made this in a hurry sorry

Yes, this would be possible, but I would add the connections into a table and disconnect them when running the function. ex:

local connections = {}

function loop()
    for i,v in pairs(connections) do 
        v:Disconnect()
    end
    
    for i,v in pairs(something) do
        local conn
        
        conn = v.MouseButton1Up:Connect(function()
            loop()
        end)
        
        table.insert(connections, conn)
    end
end

This is just to prevent overlapping functions, which would cause errors/duplications in code

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.