Why does it put all players into the team and teleports them 1 by 1 instead of all at once

So i made this code at the end of my round script, what im trying to achieve is make the players go into the ‘lobby’ team and use :loadcharacter to get them back to spawn.

But what currently happens is that it picks 1 player, then waits and picks another and so forth. which will eventually make my round script loop break. Does anyone know how to fix this (probably) small error?

Please let me know!

	local players = game.Players:GetPlayers()
	
			
			Status.Value = "Removing Assets..."
	        for i = 1,#players do
		     if players[i].Character ~= nil then
			players[i].Team = Teams["Lobby"]
			players[i]:LoadCharacter()
		end
		ClonedMap:Destroy()
		wait(5)
	end	
end
1 Like

Wrap the code that is inside the for loop with task.spawn function…

:LoadCharacter() is a function that yields, so you would want to do it inside of a task.spawn() or coroutine

1 Like

Could you explain Task.Spawn to me? completely new to that

It is just creating a new thread and executes the code inside it without yielding or pausing the current thread.
You can learn more about this function on its documentation page.

2 Likes

For your case, a task.spawn would allow you to run multiple lines of code at the same time. Like @EgizianoEG stated, I recommend you read the documentation page to know the details.

So, for your script I would do:

	local players = game.Players:GetPlayers()
	
			
			Status.Value = "Removing Assets..."
	        for i = 1,#players do
		     if players[i].Character ~= nil then
            task.spawn(function() -- task.spawn
			     players[i].Team = Teams["Lobby"]
			     players[i]:LoadCharacter()
            end)
		end
		ClonedMap:Destroy()
		wait(5)
	end	
end

It looks like the wait(5) statement is inside the loop, so it will execute after each player is processed. To fix this, you can move the wait(5) statement outside the loop, like this:

Copy code

local players = game.Players:GetPlayers()

Status.Value = "Removing Assets..."
for i = 1,#players do
  if players[i].Character ~= nil then
    players[i].Team = Teams["Lobby"]
    players[i]:LoadCharacter()
  end
end

ClonedMap:Destroy()
wait(5)

This will wait for 5 seconds after all players have been processed, rather than waiting after each player.

Yeah, this was another issue.
However,

Oh yeah I didn’t realize because of the weird indenting

I’ve added it above the last end right now, works fine. because else it couldnt find the ClonedMap variable :slight_smile:

Thanks for the info on task.spawn the issue is now resolved and ill make sure to read into the documentation

ive read the documentation on :LoadCharacter() and it says " Note: The function is similar to Player:LoadCharacterBlocking(), but the request is processed asynchronously instead of synchronously. This means other tasks will be able to continue while the character is being loaded, including the rendering of the game and any other tasks."

so it seams that it yielding isnt really an issue.

Yea i have tried it and you dont need the task.spawn

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