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

How can I use For Loop in my Loading Screen code?

Is there any way I can cycle through this code with a "For i,v in pairs () do" localscript code? I know I need a Debounce. But I don't how to make to make it work.

local Player = game.Players.LocalPlayer
local MenuGui = Player.PlayerGui.MenuGui
local LoadingFrame = MenuGui.LoadingScreenFrame:GetChildren()
local MenuFrame = MenuGui.MenuFrame
local TeamsFrame = MenuGui.ChangeTeamFrame

MenuGui.Enabled = true
LoadingFrame.Visible = true

LoadingFrame.Frame1.Visible = true
wait(1)
LoadingFrame.Frame2.Visible = true
wait(1)
LoadingFrame.Frame3.Visible = true
wait(1)
LoadingFrame.Frame4.Visible = true
wait(1)
LoadingFrame.Frame5.Visible = true
wait(1)

1 answer

✓ Accepted answer
local LoadingFrame = MenuGui.LoadingScreenFrame:GetChildren()

Your code will error because LoadingFrame is a table, since GetChildren returns a table of the children.

You can loop through the children of an object as such:

for i, v in pairs(object:GetChildren()) do
	print(v.Name) -- prints the name of the child
end

So to implement this into your script, you have to loop through MenuGui.LoadingScreenFrame:GetChildren(), the children of MenuGui.LoadingScreenFrame as such:

for i, v in pairs(MenuGui.LoadingScreenFrame:GetChildren()) do
	v.Visible = true
	wait(1)
end

We can rename the i and v variable to a more fitting name:

for _, frame in pairs(MenuGui.LoadingScreenFrame:GetChildren()) do
	frame.Visible = true
	wait(1)
end

i has been renamed to _ because variables named _ usually means they are not used. v has been renamed to frame because the children of Menu.LoadingScreenFrame are frames.

local LoadingFrame = MenuGui.LoadingScreenFrame:GetChildren()

In your script, you define LoadingFrame as the children of Menu.LoadingScreenFrame, but you attempt to edit the Visible property of Menu.LoadingScreenFrame incorrectly:

LoadingFrame.Visible = true

What you should do instead is define LoadingFrame as MenuGui.LoadingScreenFrame, not its children:

local LoadingFrame = MenuGui.LoadingScreenFrame

So now you're actually editing MenuGui.LoadingScreenFrame.Visible when you do LoadingFrame.Visible = true.

Your code should now look like this:

local Player = game.Players.LocalPlayer
local MenuGui = Player.PlayerGui.MenuGui
local LoadingFrame = MenuGui.LoadingScreenFrame
local MenuFrame = MenuGui.MenuFrame
local TeamsFrame = MenuGui.ChangeTeamFrame

MenuGui.Enabled = true
LoadingFrame.Visible = true

for i, v in pairs(LoadingFrame:GetChildren()) do
	v.Visible = true
	wait(1)
end

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