We all know that before a player can load into a server the script would run first. In my case, I want it to be the opposite. If a player starts a brand new server, I want the main script to wait until a player is fully loaded in and then the scripts can run. One thing I want to look out for is the fact that if someone else joins, the main script would not run again. I found the solution to it by using this piece of code at the top of my Main script:
while wait() do local players = game.Players:GetPlayers() if(#players > 0) then break end print("Nope") -- Used to check if this would keep printing until someone fully joined end
I want to know if there is an easier and faster solution to this.
It is possible to utilize the PlayerAdded event to wait for a player to join the game.
game.Players.PlayerAdded:Wait()
The :Wait() tells it to yield until the event has been fired.
With the assumption of that the script runs all first, then the player joins, the script would yield, until a player joins. Do note, that the behavior is sometimes inconsistent in studio.
On the other hand, it is currently not possible to tell from a server side prespective if a client is "Fully loaded" and ready to go. (Fully loaded in meaning of finished loading all assets.) a workaround would be to have a remote event that gets fired by the client once all their assets are finished loading.
(Assuming the RemoteEvent is placed or ReplicatedStorage) LocalScript:
local ContentProvider = game:GetService("ContentProvider") local RE = game:GetService("ReplicatedStorage"):WaitForChild("ClientReady") repeat wait() until ContentProvider.RequestQueueSize == 0 RE:FireServer()
Server script
game:GetService("ReplicatedStorage"):WaitForChild("ClientReady").OnServerEvent:Wait()
Players.NumPlayers
changed.local players = game:GetService"Players" local np = "NumPlayers" players:GetPropertyChangedSignal(np):Connect(function() if players.NumPlayers == 0 then print"nope" else -- code end end)