Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to use for loop to have each frame visible?

local gui = script.Parent.Parent
local frame = script.Parent

local buttonsFrame = frame:WaitForChild("ButtonsFrame")
local sectionFrame = frame:WaitForChild("SectionFrame")

local cashFrame = sectionFrame:WaitForChild("CashFrame")
local cratesFrame = sectionFrame:WaitForChild("CratesFrame")
local loadoutFrame = sectionFrame:WaitForChild("LoadoutFrame")
local weaponsFrame = sectionFrame:WaitForChild("WeaponsFrame")

for _, v in pairs(buttonsFrame:GetChildren()) do
	if v:IsA("TextButton") then
		v.MouseButton1Down:connect(function()
			if v.Name == "CloseButton" then
				gui:Destroy()
			else
				-- I want this section to be shorter --
				for _, i in pairs(sectionFrame:GetChildren()) do
					if v.Name == "WeaponsButton" then
						weaponsFrame.Visible = true
						cratesFrame.Visible = false
						cashFrame.Visible = false
						loadoutFrame.Visible = false
					elseif v.Name == "CratesButton" then
						cratesFrame.Visible = true
						weaponsFrame.Visible = false
						cashFrame.Visible = false
						loadoutFrame.Visible = false
					elseif v.Name == "CashButton" then
						cashFrame.Visible = true
						cratesFrame.Visible = false
						weaponsFrame.Visible = false
						loadoutFrame.Visible = false
					elseif v.Name == "LoadoutButton" then
						loadoutFrame.Visible = true
						cratesFrame.Visible = false
						cashFrame.Visible = false
						weaponsFrame.Visible = false
					end
				end
			end
		end)
	end
end

Is there a way to make this shorter? I tried adding a for loop to go through the SectionFrame and :GetChildren() but I don't know how I could set the right frame to be visible. Any ideas?

1 answer

✓ Accepted answer

This.

for _,i in pairs(sectionFrame:GetChildren()) do

	--set everything false
	cratesFrame.Visible = false
	cashFrame.Visible = false
	loadoutFrame.Visible = false
	weaponsFrame.Visible = false


    --set true whatever has to be true
	if v.Name == "WeaponsButton" then
		weaponsFrame.Visible = true
	elseif v.Name == "CratesButton" then
		cratesFrame.Visible = true
	elseif v.Name == "CashButton" then
		cashFrame.Visible = true
	elseif v.Name == "LoadoutButton" then
		loadoutFrame.Visible = true
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.