How to make the in Ipairs loops run at the same time

I made this script but the countdown keeps glitching and I don’t know how to fix it.

while wait() do
	for index, object in ipairs(game.Players:GetDescendants()) do
		coroutine.wrap(function()
			if object.Name == "IntermissionText" then
				for i = 30, 0, -1 do
					object.Text = "INTERMISSION - ".. i
					wait(1)
				end
			end	
		end)()	
	end
end

It does that for both players

Thank you for helping

You don’t have a wait time on your outermost loop, so its launching the functions faster than you want.

the while wait() is spamming all players with the following function, as fast as it can

So where do I put the wait()? And should I place the coroutine function somewhere else?

you don’t need a coroutine or a while loop, remove those


for i = 30, 0, -1 do
	for index, object in ipairs(game.Players:GetDescendants()) do
		if object.Name == "IntermissionText" then	
			object.Text = "INTERMISSION - ".. i	
		end
	end
	wait(1)
end

FYI, may or may not work, i’m typing in a notepad, not in studio,
also, I would not do it this way, i was just trying to rearrange your code.

I think you’re looking for something like this:

local Players = game:GetService("Players")

Players.DescendantAdded:Connect(function(instance)
	if instance.Name == "IntermissionText" then
		while true do
			print("Intermission started")

			for i = 30, 0, -1 do
				instance.Text = "INTERMISSION - "..i
				task.wait(1)
			end

			print("Game started")

			for i = 60, 0, -1 do
				instance.Text = "GAME - "..i
				task.wait(1)
			end
		end
	end
end)

BTW, I don’t recommend storing Instances inside of the Players service. Players service is meant to have only Player-type Instances as children


@Official_1z1z1z1 I’ve replaced the code with a new version that will detect when players join the game

Original code
local Players = game:GetService("Players")

local function intermissionLoop(textLabel)
	while true do
		print("Intermission started")

		for i = 30, 0, -1 do
			textLabel.Text = "INTERMISSION - "..i
			task.wait(1)
		end

		print("Game started")

		for i = 60, 0, -1 do
			textLabel.Text = "GAME - "..i
			task.wait(1)
		end
	end
end

for _, instance in Players:GetDescendants() do
	if instance.Name == "IntermissionText" then
		task.spawn(intermissionLoop, instance)
	end
end
1 Like

I suppose you could use the coroutine on the ‘for index…’ loop, to keep the timing more accurate, but I’m not sure if the 30 seconds accuracy is that important. but be sure to keep the wait(1) outside of that!

He is searching for the textlabel in a playergui which is in the player. (not a good method, but the playergui is already in there, he isn’t really storing anything that shouldn’t be there)

1 Like

there isn’t any need for coroutines, its just unnecessary thread creation that serves no benefit

agree, but if his function takes too long, his 30 second counter turns into 30.0001 (oh no!)

but is it possible to make countdown the same for everyone even if someone joins later?

1 Like

Yes, I’ll work on an example and will edit this reply when done :slight_smile::+1:


@Official_1z1z1z1 This should be synchronized for all players:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local i = 30

local text = "INTERMISSION - "

RunService.Heartbeat:Connect(function(deltaTime: number)
	i -= deltaTime

	local x = math.floor(i)

	if x == 0 then
		if text == "INTERMISSION - " then
			i = 60
			text = "GAME - "
		else
			i = 30
			text = "INTERMISSION - "
		end
	end

	for _, instance in Players:GetDescendants() do
		if instance.Name == "IntermissionTime" then
			instance.Text = text..x
		end
	end
end)

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