It takes all the players and assigns them a variable because I need it to do that
The issue, is that it comes out as nil because the player’s dont load in time
I have found this:
local Game = game
local Players = Game:GetService("Players")
repeat
Players.PlayerAdded:Wait()
until #Players:GetPlayers() == 5 --Wait for 5 players.
it waits until all the players are loaded, but the issue is that you have to put in a number of how many players it should wait for, but that’s not always gonna be the same in the game
Hey @Drixitty, This script is an Simpler version of the script you found and it uses the PlayerAdded event of the Players service to detect when a player is added to the game then it waits for the player’s character to be loaded using the CharacterAdded event.
local Players = game:GetService("Players")
local function onPlayerAdded(player)
local character = player.Character or player.CharacterAdded:Wait()
-- Your code here
end
Players.PlayerAdded:Connect(onPlayerAdded)
local PlayersWithoutCharacters = {}
local EveryoneLoadedEvent = Instance.new("BindableEvent")
local plrs = game:GetService("Players")
local FirstPlayerLoaded = false
plrs.PlayerAdded:Connect(function(plr)
table.insert(PlayersWithoutCharacters,plr)
plr.CharacterAdded:Connect(function()
index = table.index(PlayersWithoutCharacters,plr)
if index then
table.remove(PlayersWithoutCharacters,plr)
end
end)
FirstPlayerLoaded = true
end)
local WaitingFunction = game:GetService("RunService").Heartbeat:Connect(function()
if FirstPlayerLoaded then
if #PlayersWithoutCharacters < 1 then
EveryoneLoadedEvent:Fire()
WaitingFunction:Disconnect()
end
end
end)
This might not be possible if your game does not use a logging system before a game cycle starts because people can join a server at any time. You will probably need a voting system with a timer that logs which players are going to join the game cycle. After the timer runs out you can close the voting system and be sure of which players are loaded and available for the game cycle to begin.
If this is not possible you may have to choose a minimum player count, i.e.:
until #Players:GetPlayers() >= 5 -- or whatever min you choose