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.
CharacterAdded is not a valid event of Players; you attempt to call RBXScriptSignal:Wait()
on it despite this, which causes an error.
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.
ServiceProvider:GetService()
to get all services except Workspace
, which can be got using the built-in variable workspace or the DataModel.Workspace
property.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.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!