Periodically, assuming a worst-case scenario, I want to teleport 150 players across different parts of the map simultaneously without it failing, while a few other codes would simultaneously run to reset other data.
The code currently flows like this:
- Server initiates the teleportation gating process (checks if they are eligible, etc.), and drops non-eligible teleportations
- Server jumps the player out of a seat if they are in one
- Server repeatedly tries to move the player to the designated location in the map, and retries every 0.5 seconds until either a) teleportation is successful or b) the timeout has reached
- Repeat step 1-3 for each eligible player until all players are teleported
There are a few problems I have seen when teleporting that large number of players at once:
We have to jump the player out of the seat if they are in once to prevent physics issues. However, simply jumping them once sometimes fails due to lag between client and server, whether that is physics, scheduler being overwhelmed, or simply just the network.
Attempting to teleport them on the client solely sometimes results in a desync problem between the client and server. Client would see themselves somewhere else, where the server (or another player, for that matter) may see them elsewhere. Moving teleportation (i.e. the
MoveTocall) to the server solves that issue, but in turn becomes a performance bottleneck for all other scriptsMoving them into the task scheduler using
task.spawnandtask.deferdoes not guarantee any success when it is run only one time. The workaround I attempted was to repeatedly teleport the player until they were close enough to the destination. However, there are two issues:
- If one loop takes longer to complete, the other tasks (teleport or other non-related codes) cannot complete in a timely manner. Therefore creating a bottleneck.
- For performance reasons, I obviously can’t have teleport retry infinitely. There’s an arbitrary timeout of 5 seconds before the teleportation process gives up. However, a higher number tends to have higher teleportation success but results in some decreased performance, whereas a lower one tends to be faster but results in a higher number of failed teleportations.
Teleporting that many players at once and having it succeed is an integral part of the game’s mechanism. I can’t run it in parallel because
MoveTo()or any CFrame-based operations are not safe in parallel.
I’m reaching out because, surely, there’s a better way to do it than the way I have described above, right?