Make Sure Server Script Runs Before Player Join?

So I want to paste something in StaterPlayerScripts as soon as the server starts so the first player who joins will be able to automatically get those scripts.

How can I do this?

I’m unsure about what you specifically want to do.

Do you just want the first player to get them?

Please elaborate.

A simple way would be using the Players.PlayerAdded event to track when a player joins.

Then just put a Boolean variable like “FirstPlayer = true” in the script. Whenever PlayerAdded fires, check to see if FirstPlayer is true. If it is, set it to false then give the player whatever you want.

So when any player joins, this event fires. It checks FirstPlayer for every player, and since it’s true when the script first runs, the first player to join will get the stuff while FirstPlayer is set to false, meaning no one else gets it.

Does this help?

1 Like

Hey, Im on mobile at the moment, but I do understand exactly what you mean.

I have a similar situation with my admin system where some players will join the game before the scripts have been added to the correct services, such as StarterPlayerScripts.

To solve this, I copy a premade localscript into the playerguis of the first people in the server. This localscript waits a few seconds, checks for any missing scripts then clones them in accordingly.

Check out the server ‘Player’ module in the HD Admin Main Module if you’d like an example of how this is done. I’ll send you more details tomorrow if you still need assistance.

How often are players added to the game before PlayerAdded is set up, because I might need to modify my game’s server scripts if this is the case.

Edit: I think I found what ForeverHD is referring to, but this only applies to Play Solo…
https://developer.roblox.com/api-reference/event/Players/PlayerAdded

1 Like

In the case a player spawns before PlayerAdded is set up, could you use something like this?

repeat 
wait()
until game.Players:FindFirstChildOfClass("Player")

I could also just call whatever function PlayerAdded has when the game starts on Players:GetPlayers():

for _, player in pairs(game.Players:GetPlayers()) do
    OnPlayerAdded(player)
end

then connect PlayerAdded…

So if i’m interpreting this correctly, this is only a problem with studio?

Apologies, this only applies If you’re adding in scripts to ServerScriptService after the game starts. Apart from this, PlayerAdded will work perfectly fine on the server.

3 Likes

Yes, you can only use Play Solo in studio, so the problem only applies to studio.

So if im adding things to StarterPlayerScripts at the start of the server,

it can be guaranteed that any player who joins will get those scripts in online mode only

It is kind of like how the chat sometimes does not work inside studio mode, but it works every time in game.

Can you explain why you are adding scripts to this service when the game starts. Why not place them in this service beforehand?

If I could have placed them in the service before hand this thread would have never existed.

Your best bet is to not use StarterPlayerScripts at all, then. Put the scripts in ServerStorage or something similar and use another script to automatically parent them to the first player to join.

I figured out a solution already don’t worry

Just have it so players update their PlayerScripts

The way you could get StarterPlayerScripts to work, is to simply disable AutoLoadCharacter in the Players Service. Then you could do whatever you want on the server first… and then spawn the player in manually after that.

It would be much easier to just tell the server to put the scripts where its needed and then after the scripts are inside of StarterPlayerScripts you could set AutoLoadCharacter in the Players Service to true, meanwhile loop through the players already in the game, and call LoadCharacter() on them manually. All future players should automagically load in. Emphasis on “should”.

Also if you want people to stop presenting solutions. You can just reply with your own solution that worked, and set that as the solution. People are less likely to reply at that point.

That doesn’t work for StarterPlayerScripts I already tried that

If you already have a solution, please mark it as the solution, so that nobody else continues the thread.