Trying to get the previous index value in a for loop

Hey, im trying to get a script which will make 1 of 3 frames visible with math.random, which works, but what doesnt work is trying to make the previous frame unvisible

local plr = game.Players.LocalPlayer
local GameGUI = plr.PlayerGui:WaitForChild("GameGUI")
local UIS = game:GetService("UserInputService")

local Mineables = GameGUI.Background:GetChildren()
print(Mineables)

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local n = math.random(1,3)
		for i,v in Mineables do
			if n == i then
				print(Mineables[i])
				Mineables[i].Visible = true
			
			end
		end
	end
end)

Maybe add a variable for the chosen frame and using that, hide the frame:

local plr = game.Players.LocalPlayer
local GameGUI = plr.PlayerGui:WaitForChild("GameGUI")
local UIS = game:GetService("UserInputService")

local Mineables = GameGUI.Background:GetChildren()
print(Mineables)
local previousframe
UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then

		if previousframe then
		    previousframe.Visible = false
		    previousframe = nil
		end

		local n = math.random(1,3)
		for i,v in Mineables do
			if n == i then
				print(Mineables[i])
				Mineables[i].Visible = true
				previousframe = Mineables[i]
			end
		end
	end
end)
1 Like

Ah yeah, at first it didnt work because i did Mineables[i] = previousframe but i fixed it, and now it works perfectly, thank you alot

1 Like