Why won't this for loop work more then once?

I’m trying to make a script that teleports players to a certain part, but it only works once and then stops working. I’ve tried searching it up on youtube but got nothing.

local RS = game.ReplicatedStorage
local Maps = RS.Maps
local CountDownTime = 5 --Time until a round starts
local RoundEndTime = 5 -- Time for a round
local TimeValue = script.TimeValue
local Mapstoload = Maps:GetChildren()
local randomMap
local players = game.Players:GetChildren()

function roundIntermission()
	TimeValue.Value = CountDownTime
	for count = 1,CountDownTime do
		TimeValue.Value = TimeValue.Value - 1
		wait(1)
	end
end

function getRandomMap()
	randomMap = Mapstoload[math.random(1, #Mapstoload)]
	randomMap.Parent = game.Workspace
	for i,v in pairs(players) do
		players.Character.HumanoidRootPart.CFrame = randomMap.spawnPart.CFrame
	end
end

function RemoveMap()
	randomMap.Parent = RS
	for i,v in pairs(players) do
		players.Character.HumanoidRootPart.CFrame = game.Workspace.LobbySpawn.CFrame
	end
end

function roundTime()
	TimeValue.Value = RoundEndTime
	for count2 = 1, RoundEndTime do
		TimeValue.Value = TimeValue.Value - 1
		wait(1)
	end
end

while wait() do
	roundIntermission()
	getRandomMap()
	roundTime()
	RemoveMap()
end
1 Like

It only loops once because you’re calling the function once I believe? Also, try not to use while wait do.

Could you try changing the last 6 lines to

while true do
	print("First Stage")
	roundIntermission()
	print("Second Stage")
	getRandomMap()
	print("Third Stage")
	roundTime()
	print("Fourth Stage")
	RemoveMap()
	print("Restarting")
	wait()
end

for debugging, I also changed while wait() do to while true do wait()

2 Likes

The rest of the script works, it just doesn’t teleport the players from the for loop more then once

1 Like
local players = game.Players:GetChildren()

Try to use Players:GetPlayers() over GetChildren()

for i,v in pairs(players) do
	players.Character.HumanoidRootPart.CFrame = game.Workspace.LobbySpawn.CFrame
end

Players is what your looping through. If you do this, I’m certain an error will come up. v is one of the objects in the players table, so instead change the v ’s CFrame.
Hopefully this helped a bit.

1 Like

Once again, replace players.Character with v.Character.
Players is the table being looped through, and if you try to access “character” which obviously doesn’t exist it will probably return referencing a entry that doesnt exist.

1 Like

You shouldn’t do this in a variable outside of a function. This is because you’re getting the list of players at that time, meaning if a player leaves or joins it doesn’t update. You should do that every time the function is called.

function getRandomMap()
	randomMap = Mapstoload[math.random(1, #Mapstoload)]
	randomMap.Parent = game.Workspace
    local players = game.Players:GetPlayers()
	for i,v in pairs(players) do
		v.Character.HumanoidRootPart.CFrame = randomMap.spawnPart.CFrame
	end
end

function RemoveMap()
	randomMap.Parent = RS
    local players = game.Players:GetPlayers()
	for i,v in pairs(players) do
		v.Character.HumanoidRootPart.CFrame = game.Workspace.LobbySpawn.CFrame
	end
end
1 Like

Ok, that works but whenever the player is moving they don’t teleport (An example is when I just started running and jumping, it wouldn’t teleport)