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
Hey! If its a party system then you have to send the players in the current party/lobby/round… whatever, to the other server via using TeleportOptions! Then from there you can check if they all loaded it… But some may not, some will haha.
player joins and dynamically build the players table
local Players = game:GetService("Players")
local players = {}
Players.PlayerAdded:Connect(function(player)
table.insert(players, player)
end)
repeat
task.wait(1)
until #players == 5 --not sure what you mean by it may not be the same
Can see a few problem with this however… Think dduck5tar solution would be a bit less error-prone.
A player may quit the game before 5 are set up…
This is something I learned recently, but if you use the service, the service yeilds until the character is loaded.
-- Connect the function to the Players.PlayerAdded event
game.Players.PlayerAdded:Connect(function(player)
-- Wait for the player's character to load
player.CharacterAdded:Connect(function()
--logic here
end)
end)
What this does is it yeilds the rest of it until that character is added, and when it is things SHOULD be fine. Note things can happen, some people have long load times and are on iphone8’s or something so… even though this is a “solution” you should go deeper into solving it. This is where I think the other guy Pepsi_cat is right, BindableEvents are king.
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid=character:WaitForChild("Humanoid")
--
end)
end)
I like to do this. There is a slight stall between the character loaded and the description being added to it. humanoid being used here for that stall. Description added takes around 0.03
Yeah of course, but as long as you use the service! It will yeild until the whole character is added, meaning if you run the logic after you are good so yes this will work too.
Not always… I have ran into problems with this and now use this. That’s how I know the time it takes.
My player has many layers of items and an animated face … but so might others.
There is a command called CharacterAppearanceLoaded:Wait() but it doesn’t always work.
Well you do understand studio, sometimes I load my character way before my server loads. Weird things happen in studio especially if you load “too fast” and the other way around. I have had it to where I load into studio and my character didn’t load. But this method is what I have seen on the dev documentation. But either way, it will work. TO be extra safe you can even wait or do task.delay() that is the safest option.
It could be harder to maintain and update multiple places. Check TeleportData/TeleportOptions in the roblox documentation. I personally send over the amount of expected players during the teleport.