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

How to make the main script wait until there is at least 1 player loaded in the server?

Asked by 5 years ago
Edited 5 years ago

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.

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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()
1
This waits for the event to fire once, then doesn’t wait anymore. User#19524 175 — 5y
0
@incapaz That's how the asker wanted, right..? NathanAdhitya 124 — 5y
0
OP would have to use a loop for it to keep checking. User#19524 175 — 5y
0
"One thing I want to look out for is the fact that if someone else joins, the main script would not run again." OP said to only execute it on the first player join., NathanAdhitya 124 — 5y
View all comments (8 more)
0
What if another player joins? Would the main script still wait for the other person to join? I have not tried it yet. Also, does this method makes sure that it waits until the player has fully joined and is fully loaded? skate992 57 — 5y
0
@Nathan the first person to join is likely OP for testing. User#19524 175 — 5y
0
First question, main script only waits for a player to join, when it does, it continues the code. second question, Currently, you can't assure if a player is fully loaded from the server side of prespective. You'll have to go with remote events firing from the client once the client has all the needed asset loaded. NathanAdhitya 124 — 5y
0
Let me add that to the answer as well.. NathanAdhitya 124 — 5y
0
@incapaz, it's what the OP wanted. OP wants a script to be executed when a player has joined and fully loaded ONCE. When any other joins, he does not want it to be executed. NathanAdhitya 124 — 5y
0
@Nathan What is the "Content Provider"? It's the first I am seeing of it skate992 57 — 5y
0
On the client, content provider is ... a content provider. the RequestQueueSize tells how much assets are still being queued on the client. In this case, it waits until it's 0, which means all needed assets are loaded. NathanAdhitya 124 — 5y
0
Interesting. This will be useful later on. Thank you for your answer skate992 57 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

You could check if 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)

Modify it to your needs.

0
What is "GetPropertyChangedSignal(np)"? skate992 57 — 5y

Answer this question