Timer isn't restarting after running through a for in pairs() loop

Hello all, so I basically have this script set up to be a sort of time trial, I’m new to this so i’m starting with the basics but the problem i’m coming across is when I have two players in the game, one gets teleported etc. and the time stops and get shown via the remote event, but when the second player is teleported and begins, the timer doesn’t restart and stays at whatever the previous player stopped at. I have a feeling this is a problem with my for in pairs loop, because that doesn’t seem to be the best option and i still need to add stuff, but could someone explain why the time doesn’t restart.

the time is stored in an intValue in replicated storage, and is working up until the second loop in the for in pairs.

local pl = game.Players
local gameStatus = game.ReplicatedStorage.gameStatus
local time = game.ReplicatedStorage.time
local raceStart = game.Workspace.raceStart
local raceEnd = game.Workspace.raceEnd



game.Players.PlayerAdded:Connect(function(player)
	local character = player.CharacterAdded:wait()
	print(character.Name.."'s character has been found")
	if #pl:GetChildren() >= 2 then
		wait(3)
		for i, v in pairs(game.Players:GetChildren()) do
			if v then
				wait(1)
				v.Character.HumanoidRootPart.Position = game.Workspace.raceStart.Position
				print(v.Name.." has started the race")
					gameStatus.Value = "raceStarted"
						time.Value = 0
						repeat 
							time.Value = time.Value + 1
							print(time.Value)
							wait(1)
						until gameStatus == "raceEnded"	
				end
			end
		else
			print("not enough players in the game!")
	end
end)

raceEnd.Touched:Connect(function(char)
	gameStatus = "raceEnded"
	print(time.Value)
	local player = game.Players:GetPlayerFromCharacter(char.Parent)
	print(player.Name.." scored a time of"..time.Value)
	game.ReplicatedStorage.showTime:FireClient(player, time.Value)
end)

Well I can tell you the reason why it happens

Your for loop passes through each element of game.Players:GetChildren() but it has to go through the first iteration i.e. i=1 but look in your for loop, there is a repeat wait so the next iteration can never get their stuff started (i=2) until the until condition is met

the key is to create a multi threaded for loop:

coroutine.wrap(function()
      while wait(1) do
         print("Hi")
         myPart:Clone()
      end
end)()

implementing coroutine.wrap inside your for loop (wrapped around the entirety of the for loop’s innards) will solve your issue @SPR1NG3R , all iterations will be affected practically at the same time

thank you very much, I didn’t really think about the fact that i never even finished the first for loop. I appreciate the help!