I’m making a game where I need to respawn all of the players at the exact same time.
The way I’ve been respawning people was by using “:LoadCharacter()” after getting all players but I noticed that it respawns everyone one by one, waiting until each character is loaded in before respawning the next one.
This might look like me being a perfectionist but this can potentially cause bugs due to how my game works in concept.
I need everyone to respawn at the exact same time. (minor delays are okay but right now the script waits for each individual character to load before moving onto the next one)
No, this isn’t lag either. If I just kill each player at the same time, it is able to spawn them all perfectly.
local PlayerService = game:GetService("Players")
for _, v in PlayerService:GetPlayers() do -- get all players
v:LoadCharacter() -- load char for each player
end
If you don’t want to use that, you can do something like a RemoteEvent, but I wouldn’t recommend that…
If you do, add a LocalScript to the StarterCharacterScripts with the following
LocalScript in StarterCharacterScripts
local remote = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") -- get remote
local function loadPlayer() -- func to load player
game.Players.LocalPlayer:LoadCharacter() -- load localplayer
end
remote.OnClientEvent:Connect(loadPlayer) -- run function on client event from the server
In a script from the server;
Server Script
local remote = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") -- get remote
local function reloadAll() -- func for reloading all
remote:FireAllClients() -- fire all clients, which will trigger the localscript above
end
reloadAll() -- call function however you want
The first method causes the issue I described above. The second one is not something I’d like to do either. I guess I’ll have to kill the players with 0 waiting time or something.
Trying to load them all at the same time sort of puts them in this queue to respawn while killing them just respawns them without doing so…
Alright, I experimented a little and turns out I have to use different scripts for each individual loading. Decided to copy and paste a script inside every player whenever I need to load. The pasted script then loads the character of the player it’s dedicated to.
Thanks a lot for trying to help, I appreciate it a lot
The delay you’re noticing occurs because Player:LoadCharacter() is a yielding function, which means that it pauses the current thread until it returns. A common solution is to load each character in a separate thread via the task library.
local Players = game:GetService("Players")
for _, player in Players:GetPlayers() do
task.spawn(player.LoadCharacter, player)
end
Your workaround works because each script runs in its own thread. Read more about scheduling here, and look into concurrency if you’re especially interested.