Making this repeat for a amount of time

  1. What do you want to achieve? Keep it simple and clear!
    I want to make this line of code below repeat itself for lets say 10 minutes, then stop and continue the rest of the script in the entire script.
  2. What is the issue? Include screenshots / videos if possible!
    It seems that everything i try for this fails.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Have tried remove event activating while its in another script and on a while true do loop.

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!
Code:


local players = game:GetService("Players") 

function TeleportPlayers(map) 
		local teleportpads = ChosenMapB:WaitForChild("PlayerSpawns"):GetChildren()

	
	for i, player in pairs(players:GetPlayers())  do
		local numberchooser = math.random(1, #teleportpads) 
		
		local character = player.Character or player.CharacterAdded:Wait() 
		character:PivotTo(teleportpads[numberchooser].CFrame)
	end


		TeleportPlayers(ChosenMapB)   
		wait(600)
	end

Entire Script:

-- Player Countdown
while true do
local availableplayers = {}
repeat
	availableplayers = {}
	-- Checks if player is In the menu, and inserts only available players.
	for i, plr in pairs(game.Players:GetPlayers()) do
		if not plr:FindFirstChild("InMenu") then
			table.insert(availableplayers, plr)
		end
	end 
	wait(2)
until #availableplayers >= 1 

	
	
wait(15) 
warn("Starting game up as players neccessary is here.") 
wait(3) 
warn("Choosing random map!")


-- Choosing a map

	local Maps = game.ServerStorage.Maps:GetChildren()
	local ChosenMap =	Maps[math.random(1, #Maps)]
	local ChosenMapB = ChosenMap:Clone() 
	ChosenMapB.Parent = workspace
wait(0.1) 
warn("Map Chosen: "..ChosenMapB.Name)

-- Announcement to players that the game will start soon.
local ReplicatedStorage = game.ReplicatedStorage 
local event = ReplicatedStorage.Announcement 
event:FireAllClients() 
wait(5) 
local event2 = ReplicatedStorage.CallOfAnnouncement 
event2:FireAllClients()  
wait(3) 


-- Teleporting Players to game.
while true do
local players = game:GetService("Players") 

function TeleportPlayers(map) 
		local teleportpads = ChosenMapB:WaitForChild("PlayerSpawns"):GetChildren()

	
	for i, player in pairs(players:GetPlayers())  do
		local numberchooser = math.random(1, #teleportpads) 
		
		local character = player.Character or player.CharacterAdded:Wait() 
		character:PivotTo(teleportpads[numberchooser].CFrame)
	end
end

		TeleportPlayers(ChosenMapB)   
		wait(600)
	end



-- Intiate Timer 

wait(600) 

-- Intiating Round Cleanup 

	ChosenMapB:Destroy()   
	wait(5) 

	end 




Any help with this is highly appreciated.

2 Likes

Firstly, you should probably use task.wait() instead of wait() since it’s more accurate and more performant.

Secondly, I can’t understand the code. Could you explain what you’re trying to do here?

Im trying to repeat the first code i listed until a certain amount of time has been passed for example 10 minutes, then the rest of the script continues.

task.wait() is better?

You can do:

for i = 1,600 do
	TeleportPlayers(map)
	task.wait(1)
end

And yes, a staff member recommend you use task.wait() instead.

Also, you don’t have to make a variable for the RNG. Just put it in the brackets like this:

for i, player in pairs(players:GetPlayers())  do
		local character = player.Character or player.CharacterAdded:Wait() 
		character:PivotTo(teleportpads[math.random(1,#teleportpads)].CFrame)
	end

ok, so now it does not teleport the players at all. is there something i did wrong here?

local players = game:GetService("Players") 

function TeleportPlayers(map) 
		local teleportpads = ChosenMapB:WaitForChild("PlayerSpawns"):GetChildren()

	
	for i, player in pairs(players:GetPlayers())  do
		local numberchooser = math.random(1, #teleportpads) 
		
		local character = player.Character or player.CharacterAdded:Wait()
		character:PivotTo(teleportpads[numberchooser].CFrame)
			
		end 
		for i = 1,600 do 
			TeleportPlayers(ChosenMapB)   
		end
			

end

Is there an error? Also you aren’t even using the map parameter anywhere in the function.

No error, i tried fixing it more.

local players = game:GetService("Players") 

function TeleportPlayers(map) 
		local teleportpads = ChosenMapB:WaitForChild("PlayerSpawns"):GetChildren()

	
		for i, player in pairs(players:GetPlayers())  do
			local character = player.Character or player.CharacterAdded:Wait() 
			character:PivotTo(teleportpads[math.random(1,#teleportpads)].CFrame)
		end
		for i = 1,600 do
			TeleportPlayers(map)
			task.wait(1)
		end
			

end

Edit: i forgot to include this bit

	local Maps = game.ServerStorage.Maps:GetChildren()
	local ChosenMap =	Maps[math.random(1, #Maps)]
	local ChosenMapB = ChosenMap:Clone() 
	ChosenMapB.Parent = workspace
wait(0.1) 
warn("Map Chosen: "..ChosenMapB.Name)

thats in the script. It does not need to be included in the other code, but it seems good to include where ChosenMapB comes from.

You can’t call a function inside itself.

function TeleportPlayers(map) 
	local teleportpads = map:WaitForChild("PlayerSpawns"):GetChildren()

	for i, player in pairs(players:GetPlayers())  do
		local character = player.Character or player.CharacterAdded:Wait() 
		character:PivotTo(teleportpads[math.random(1,#teleportpads)].CFrame)
	end
end
for i = 1,600 do
	TeleportPlayers(ChosenMapB)
	task.wait(1)
end

This should work, I don’t see a problem with this.

1 Like

It works! Perfect! Im trying to make it to where it only functions when a player dies, because right now it just teleports you to one of the spawns every second, (AS IT SHOULD).

You can do

local canTeleport = true
players.PlayerAdded:Connect(function(player)
	if not canTeleport then return end
	local teleportpads = map:WaitForChild("PlayerSpawns"):GetChildren()

	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			-- respawn character here
		end)
	end)
end)

But why don’t you set the player’s team and set each respawn point to their team? It should automatically respawn them randomly for you.

local players = game:GetService("Players") 

	function TeleportPlayers(map) 
		local teleportpads = map:WaitForChild("PlayerSpawns"):GetChildren()

		for i, player in pairs(players:GetPlayers())  do
			local character = player.Character or player.CharacterAdded:Wait() 
			character:PivotTo(teleportpads[math.random(1,#teleportpads)].CFrame)
		end
	end
	for i = 1,600 do
		TeleportPlayers(ChosenMapB)
		task.wait(1)
	end

where would i put the code in here? bit confused on that, plus would i make it go like this:

local canTeleport = true
players.PlayerAdded:Connect(function(player)
	if not canTeleport then return end
	local teleportpads = map:WaitForChild("PlayerSpawns"):GetChildren()

	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			TeleportPlayers(ChosenMapB)
		end)
	end)
end)

?

Sorry for all of the quesions i am a very bad scripter and i get confused pretty easily.

its a ffa, there are no teams so i want all players to just randomly respawn anywhere where the spawns are on death.