Cant figure out how to loop this

I’m trying to make this loop but can’t figure it out for the life of me.
This is my script:

script.Parent.MouseButton1Click:Connect(function()
script.Parent.Parent.Team2Frame.Visible = true
script.Parent.MouseButton1Click:Connect(function()
script.Parent.Parent.Team3Frame.Visible = true
wait(.1)
script.Parent.Parent.Team2Frame.Visible = false
script.Parent.MouseButton1Click:Connect(function()
script.Parent.Parent.Team4Frame.Visible = true
wait(.1)
script.Parent.Parent.Team3Frame.Visible = false
wait(.1)
script.Parent.Parent.Team2Frame.Visible = false
script.Parent.MouseButton1Click:Connect(function()
script.Parent.Parent.Team1Frame.Visible = true
wait(.1)
script.Parent.Parent.Team4Frame.Visible = false
wait(.1)
script.Parent.Parent.Team3Frame.Visible = false
wait(.1)
script.Parent.Parent.Team2Frame.Visible = false
end)
end)
end)
end)

pretty much what it does is switches frames every time you click a button. Is it possible to make it so once it gets to the last frame and you click the button again, it starts back at the 1st frame?

Might be easier if you just had some click index, and let it go up to 4, once it reaches 4 reset it back to 1, and inside your first mouse click, just use that and put in a few if statements on what to do. Your current method will end up having too many connections to the button and since they don’t un-register the last connection it’s basically running each mouse click function when you click it once.

try something like…

local ClickCount = 1
script.Parent.MouseButton1Click:Connect(function()
    if ClickCount == 1 then
        --Make frames visible or invisible
        ClickCount = 2
    elseif ClickCount == 2 then
        --Make frames visible or invisible
        ClickCount = 3
    elseif ClickCount == 3 then
        --Make frames visible or invisible
        ClickCount = 4
    elseif ClickCount == 4 then
        --Make frames visible or invisible
        ClickCount = 1
    end
end)
1 Like