Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Best way to wait for all players to load in then do something?

Asked by 5 years ago
Edited 5 years ago

I need a way to wait for all the players to be loaded in then do my stuff.

This is what I have at the moment:

local RepStorage = game:WaitForChild("ReplicatedStorage")
local Remote = RepStorage:WaitForChild("GameRemote")

--Wait for players
local WaitingPlayers = game:GetService("Players")
WaitingPlayers.CharacterAdded:Wait()
wait(1)
--FireClient
Remote:FireAllClients()
print("Countdown has been fired")
--FireClient
Remote.OnServerEvent:Connect(function(player)
    local Players = game.Players:GetPlayers()
    local PlayerAmount = tonumber(#Players)
    print(PlayerAmount)
    print("Create Rafts")
    for i = 1,PlayerAmount do
        wait(0.1)
        local Raft1 = game.ReplicatedStorage.Minigames.StartingMinigame.CloneableRaft:Clone()
        Raft1.Parent = game.Workspace.Minigames.RaftRace.Rafts
        Raft1.Name = "Raft1"
        Raft1:MoveTo(Vector3.new(323.95, 0.261, -63.462))
        game.Workspace[player.Name].HumanoidRootPart.CFrame = CFrame.new(Raft1.Spawn.Position)
        wait(0.1)
        game.Workspace[player.Name].Humanoid.WalkSpeed = 0
        game.Workspace[player.Name].Humanoid.JumpPower = 0
    end
end)

I keep on getting errors on line 6.

0
the Player service doesn't have a CharacterAdded event, each player has their own individual CharacterAdded event User#22604 1 — 5y
0
try player added then character added greatneil80 2647 — 5y

1 answer

Log in to vote
0
Answered by
BenSBk 781 Moderation Voter
5 years ago
Edited 5 years ago

The Issue

CharacterAdded is not a valid event of Players; you attempt to call RBXScriptSignal:Wait() on it despite this, which causes an error.

The Solution

An appropriate solution to this problem is to get all the players in the game, then iterate through them, checking to see whether their character is loaded or not. If the latter, we will use Player.CharacterAdded:Wait() to wait for their character to load. If the former, we will continue onto the next player.

The Nitpicks

  • On line 01, 13, and 19, you get a service via its Name property. Instead, it is recommended to use ServiceProvider:GetService() to get all services except Workspace, which can be got using the built-in variable workspace or the DataModel.Workspace property.
  • On line 14, you use tonumber() on data that will always already be a number. The # operator always results in a number, so there is no need to call this function on it.

The Code

local players = game:GetService("Players")

local function wait_for_characters(player_table) -- create a function for re-usability
    for _, player in pairs(player_table) do -- iterate through the provided players
        if not player.Character then -- nil if the Character has not yet loaded
            player.CharacterAdded:Wait() -- wait for the CharacterAdded event to fire
        end
    end
end

wait_for_characters(players:GetPlayers()) -- call our function, passing all the players in the game
print("All players' characters have loaded.")

I hope this helped!

Ad

Answer this question