How to make a for loops runs everything at the same time

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

i want to make it like i want to run a function but it runs everything at the same time

  1. What is the issue? Include screenshots / videos if possible!
    idk how
  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

there isnt any script, i just want to know how to make everything run at the same time, for example

for i,v in ipairs(game.Players:GetChildren())
    v.PlayerGui.Frame.Visible = true
    wait(1)
    v.PlayerGui.Frame.Visible = false
end

the script will run the for loop for one player at a time, but in my case i want it to run for all the players in the same time.

You need to put the wait after you checked all Players

for example in a while true. (don’t do a while true)

local Players = game:GetService("Players")

while true do
	for i, v in pairs(Players:GetChildren()) do
		v.PlayerGui.Frame.Visible = true
	end
	wait(1)
	for i, v in pairs(Players:GetChildren()) do
		v.PlayerGui.Frame.Visible = false
	end
end

You can create a function for this instead of while true

local function player_frame()
	for i, v in pairs(Players:GetChildren()) do
		v.PlayerGui.Frame.Visible = true
	end
	wait(1)
	for i, v in pairs(Players:GetChildren()) do
		v.PlayerGui.Frame.Visible = false
	end
end

player_frame()

i tried the function but it still doesnt run all the same time: here is my script that i modified because of the problem:

wait(2)
local intermissionLength = 5 -- Time in seconds of the time you in the lobby
local Status = game.ReplicatedStorage.Status
local Players = game:GetService("Players")
local InRound = game.ReplicatedStorage.InRound
local Maps = game:GetService("ServerStorage"):FindFirstChild("Maps")
local Rounds = game.ReplicatedStorage.Round

local PlayersInGame = {}

local LobbySpawn = game.Workspace.LobbySpawn

InRound.Changed:Connect(function()
	if InRound.Value == true then
		for _, player in pairs(game.Players:GetChildren()) do
			local char = player.Character
			local Clone = workspace.Colors:WaitForChild("Map")
			local randomColorchooser = math.random(1, #Clone:GetChildren())	
			local randomPart: Part = Clone:GetChildren()[randomColorchooser]
			char.HumanoidRootPart.CFrame = randomPart.CFrame + Vector3.new(0,5,0)
		end
	else
		for _, player in pairs(game.Players:GetChildren()) do
			local char = player.Character
			char.HumanoidRootPart.CFrame = LobbySpawn.CFrame
		end
	end
end)

local function RemoveRound()
	InRound.Value = false
	
	local Map: Model = workspace.Colors:FindFirstChild("Map")
	
	if Map then
		Map:Destroy()
	end
	
	
	for i,v in ipairs(Players:GetChildren()) do
		table.remove(PlayersInGame, table.find(PlayersInGame, v))
	end
end

local function EliminatePlayer(plr)
	table.remove(PlayersInGame, table.find(PlayersInGame, plr))
end

game.Players.PlayerRemoving:Connect(function(plr)
	EliminatePlayer(plr)
end)

script.Die.Event:Connect(function(plr)
	EliminatePlayer(plr)
end)

local function SetColorGui(Color, Player)
	local PlayerColorGui: Frame = Player.PlayerGui.Core.Colors
	local PlayerColorColorGui: Frame = PlayerColorGui.Color
	local PlayerColorColorNameText: TextLabel = PlayerColorGui.ColorName
	local PlayerColorTimerText: TextLabel = PlayerColorGui.Timer
	PlayerColorGui.Visible = true
	PlayerColorColorGui.BackgroundColor = Color
	PlayerColorColorNameText.Text = Color.Name
	for i = 5,0,-1 do
		PlayerColorTimerText.Text = i
		wait(1)
		if i == 0 then
			PlayerColorGui.Visible = false
			return
		end
	end
end

local function PlayerWin(Plr: Player?)
	for i,v in ipairs(Players:GetChildren()) do
		local PlayerWinnerGui: Frame = v.PlayerGui.Core.Winner
		local PlayerWinnerWinnersText: TextLabel = PlayerWinnerGui.Winners
		PlayerWinnerGui.Visible = true
		if not Plr then
			PlayerWinnerWinnersText.Text = "Nobody"
		else
			PlayerWinnerWinnersText.Text = Plr.Name
		end
		wait(3)
		PlayerWinnerGui.Visible = false
	end
end

local function StartRound(Map)
	Rounds.Value = Rounds.Value + 1
	Status.Value = "Starting New Round..."
	
	local Clone: Model
	
	if not workspace.Colors:FindFirstChild("Map") then
		Clone = Map:Clone()
		Clone.Parent = workspace.Colors
		Clone.Name = "Map"
	end
	
	local Clone = workspace.Colors:FindFirstChild("Map")
	
	InRound.Value = true
	
	Status.Value = "Round Started!"
	
	for i, v in ipairs(Clone:GetChildren()) do
		v.BrickColor = BrickColor.random()
	end
	
	local generatedrandomColor
	
	
	local randomColorchooser = math.random(1, #Clone:GetChildren())	
	local randomPart: Part = Clone:GetChildren()[randomColorchooser]
	
	generatedrandomColor = randomPart.BrickColor
	
	wait(1)
	
	Status.Value = "The generated Color is "..generatedrandomColor.Name
	for i, v in ipairs(game.Players:GetChildren()) do
		SetColorGui(generatedrandomColor, v)
	end
	
	for i, v in ipairs(Clone:GetChildren()) do
		if v.BrickColor ~= generatedrandomColor then
			v.Transparency = 1
			v.CanCollide = false
		end
	end
	
	wait(3)
	
	for i, v in ipairs(Clone:GetChildren()) do
		if v.BrickColor ~= generatedrandomColor then
			v.Transparency = 0
			v.CanCollide = true
		end
	end
end


while wait() do
	if #Players:GetChildren() >= 1 then
		if InRound.Value == false then
			for i = intermissionLength, 0, -1 do -- "-1" is the seconds the timer goes down by
				InRound.Value = false
				wait(1)
				Status.Value = "Intermission: ".. i .." Seconds.."
				if i == 0 then
					print("DEBUG")
					Status.Value = "Generating Map Please Wait."
					local RandomMapNumber = math.random(1, #Maps:GetChildren())
					local RandomMap = Maps:GetChildren()[RandomMapNumber]
					wait(1)
					Status.Value = "The chosen map is ".. RandomMap.Name
					wait(1.5)
					for i,v in ipairs(Players:GetChildren()) do
						table.insert(PlayersInGame, v)
					end
					wait(1)
					while wait() do
						if #PlayersInGame <= 0 then
							Status.Value = "Ending Round."
							wait(2)
							local PlayerWinner
							for i, v in ipairs(PlayersInGame) do
								if v == nil then continue end
								PlayerWinner = v.Name
								print(PlayerWinner)
							end
							print(PlayerWinner)
							if PlayerWinner == nil then
								Status.Value = "Nobody Won!"
								PlayerWin(nil)
							else
								local Player = game.Players:FindFirstChild(PlayerWinner)
								print(Player)
								Status.Value = "The winner is "..Player.Name
								PlayerWin(Player)
							end
							RemoveRound()
							break
						else
							StartRound(RandomMap)
						end
						wait(1)
					end
				end
			end
		end
	else
		Status.Value = "There isnt enough players to play the game, try inviting some friends or rejoin to another server!"
	end
end

here is the original script:

wait(2)
local intermissionLength = 5 -- Time in seconds of the time you in the lobby
local Status = game.ReplicatedStorage.Status
local Players = game:GetService("Players")
local InRound = game.ReplicatedStorage.InRound
local Maps = game:GetService("ServerStorage"):FindFirstChild("Maps")
local Rounds = game.ReplicatedStorage.Round

local PlayersInGame = {}

local LobbySpawn = game.Workspace.LobbySpawn

InRound.Changed:Connect(function()
	if InRound.Value == true then
		for _, player in pairs(game.Players:GetChildren()) do
			local char = player.Character
			local Clone = workspace.Colors:WaitForChild("Map")
			local randomColorchooser = math.random(1, #Clone:GetChildren())	
			local randomPart: Part = Clone:GetChildren()[randomColorchooser]
			char.HumanoidRootPart.CFrame = randomPart.CFrame + Vector3.new(0,5,0)
		end
	else
		for _, player in pairs(game.Players:GetChildren()) do
			local char = player.Character
			char.HumanoidRootPart.CFrame = LobbySpawn.CFrame
		end
	end
end)

local function RemoveRound()
	InRound.Value = false
	
	local Map: Model = workspace.Colors:FindFirstChild("Map")
	
	if Map then
		Map:Destroy()
	end
	
	
	for i,v in ipairs(Players:GetChildren()) do
		table.remove(PlayersInGame, table.find(PlayersInGame, v))
	end
end

local function EliminatePlayer(plr)
	table.remove(PlayersInGame, table.find(PlayersInGame, plr))
end

game.Players.PlayerRemoving:Connect(function(plr)
	EliminatePlayer(plr)
end)

script.Die.Event:Connect(function(plr)
	EliminatePlayer(plr)
end)

local function SetColorGui(Color, Player)
	local PlayerColorGui: Frame = Player.PlayerGui.Core.Colors
	local PlayerColorColorGui: Frame = PlayerColorGui.Color
	local PlayerColorColorNameText: TextLabel = PlayerColorGui.ColorName
	local PlayerColorTimerText: TextLabel = PlayerColorGui.Timer
	PlayerColorGui.Visible = true
	PlayerColorColorGui.BackgroundColor = Color
	PlayerColorColorNameText.Text = Color.Name
	for i = 5,0,-1 do
		PlayerColorTimerText.Text = i
		wait(1)
		if i == 0 then
			PlayerColorGui.Visible = false
			return
		end
	end
end

local function PlayerWin(Plr: Player?)
	for i,v in ipairs(Players:GetChildren()) do
		local PlayerWinnerGui: Frame = v.PlayerGui.Core.Winner
		local PlayerWinnerWinnersText: TextLabel = PlayerWinnerGui.Winners
		PlayerWinnerGui.Visible = true
		if not Plr then
			PlayerWinnerWinnersText.Text = "Nobody"
		else
			PlayerWinnerWinnersText.Text = Plr.Name
		end
		wait(3)
		PlayerWinnerGui.Visible = false
	end
end

local function StartRound(Map)
	Rounds.Value = Rounds.Value + 1
	Status.Value = "Starting New Round..."
	
	local Clone: Model
	
	if not workspace.Colors:FindFirstChild("Map") then
		Clone = Map:Clone()
		Clone.Parent = workspace.Colors
		Clone.Name = "Map"
	end
	
	local Clone = workspace.Colors:FindFirstChild("Map")
	
	InRound.Value = true
	
	Status.Value = "Round Started!"
	
	for i, v in ipairs(Clone:GetChildren()) do
		v.BrickColor = BrickColor.random()
	end
	
	local generatedrandomColor
	
	
	local randomColorchooser = math.random(1, #Clone:GetChildren())	
	local randomPart: Part = Clone:GetChildren()[randomColorchooser]
	
	generatedrandomColor = randomPart.BrickColor
	
	wait(1)
	
	Status.Value = "The generated Color is "..generatedrandomColor.Name
	SetColorGui(generatedrandomColor, v)
	
	for i, v in ipairs(Clone:GetChildren()) do
		if v.BrickColor ~= generatedrandomColor then
			v.Transparency = 1
			v.CanCollide = false
		end
	end
	
	wait(3)
	
	for i, v in ipairs(Clone:GetChildren()) do
		if v.BrickColor ~= generatedrandomColor then
			v.Transparency = 0
			v.CanCollide = true
		end
	end
end


while wait() do
	if #Players:GetChildren() >= 1 then
		if InRound.Value == false then
			for i = intermissionLength, 0, -1 do -- "-1" is the seconds the timer goes down by
				InRound.Value = false
				wait(1)
				Status.Value = "Intermission: ".. i .." Seconds.."
				if i == 0 then
					print("DEBUG")
					Status.Value = "Generating Map Please Wait."
					local RandomMapNumber = math.random(1, #Maps:GetChildren())
					local RandomMap = Maps:GetChildren()[RandomMapNumber]
					wait(1)
					Status.Value = "The chosen map is ".. RandomMap.Name
					wait(1.5)
					for i,v in ipairs(Players:GetChildren()) do
						table.insert(PlayersInGame, v)
					end
					wait(1)
					while wait() do
						if #PlayersInGame <= 0 then
							Status.Value = "Ending Round."
							wait(2)
							local PlayerWinner
							for i, v in ipairs(PlayersInGame) do
								if v == nil then continue end
								PlayerWinner = v.Name
								print(PlayerWinner)
							end
							print(PlayerWinner)
							if PlayerWinner == nil then
								Status.Value = "Nobody Won!"
								PlayerWin(nil)
							else
								local Player = game.Players:FindFirstChild(PlayerWinner)
								print(Player)
								Status.Value = "The winner is "..Player.Name
								PlayerWin(Player)
							end
							RemoveRound()
							break
						else
							StartRound(RandomMap)
						end
						wait(1)
					end
				end
			end
		end
	else
		Status.Value = "There isnt enough players to play the game, try inviting some friends or rejoin to another server!"
	end
end

that is the function im worrying about

i want the function to run for all the players at the same time, or my game will break

i think i know how to do this in a easier way using local scripts but i would like to know how (because of the title)

you are still putting in your script the wait inside the for loop in my script it’s outside

ok i just figured out a solution.

i just used a local script

thats the solution

With lua you cannot truly run things at the same time - but you can get as close as possible using coroutines, which run tasks on different “threads”

try something like

edit: I’ve just been informed there is parallel lua as of the past year, but not required here

for index, value in pairs() do
 coroutine.wrap(function()
  gui.visible = true
  wait(1)
  gui.visible = false
 end)()
end

then substitue whatever you’re doing for all players in therestrong text

i forgot that coroutine exists, i thought that task is like coroutine

im so dumb

but still, someone in another Topic said that local scripts are the best if you are dealing with UIs.

yeah that’s generally correct - keep your UI stuff in a local script, as UIs are generally unique to the player

I don’t wanna see your coins, and you don’t wanna see mine

true!

idk why i put my UIs using scripts XD

the server cant handle it

haha yep leave that to the client :sweat_smile:

just let the player gets uis everywhere just like a virus duplicating.

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