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

What does StarterPlayerScripts mean for my game?

Asked by
TMGOR 20
7 years ago

So I want to create a script (NOT A REQUEST) that brings up a GUI on when a player enters the game. I'm guessing the first step to do this is to find a local player. My first move was to create this local script:

local plr = game.Players.LocalPlayer
wait(3)
print(plr.Name) 

The coding isn't the wrong thing here, but it is where it's placed. Being a beginner my first move was to place this in Workspace and open Test, where it outputted nothing. After searching around on the Wikia I decided to place it in StarterPlayerScripts purely because it was mentioned in the article and it sounded right. Surely enough it worked, but it made me wanting to know: do I put all local scripts in StarterPlayerScripts, and what is the difference between StarterCharacterScripts? And if I was to make a GUI would I place it in ReplicatedStorage for it to be cloned into StarterPlayerScripts?

All answers will be essential for my project c=

3 answers

Log in to vote
3
Answered by
duckwit 1404 Moderation Voter
7 years ago
Edited 7 years ago

The essential trade-off you'll be weighing up is how much you rely on the current behaviour of ROBLOX vs. how much you want to be responsible for your own script and character instantiation flow. Historically, there has been very little freedom with how to organise the high-level structure of one's project, and superimposing any kind of manual script replication was a pain. Relatively new services like ReplicatedStorage, ServerScriptService, StarterPlayer, etc... have helped to remedy this a bit, though.

StarterPlayer vs. StarterCharacter

As you may have discovered on the API page for LocalScript, they will only run in 4 specific locations:

  1. A player's Backpack
  2. A player's Character Model
  3. A player's PlayerGui
  4. A player's PlayerScripts

There are 2 differences between scripts in PlayerScripts and in Character:

  1. Scripts in PlayerScripts are not visible to the server, but they are visible in Character
  2. Scripts in PlayerScripts are not reset when the player dies/re-spawns, but they are reset if parented to Character because the model is usually destroyed.

Where should I put my precious GUI elements?

Whatever you do, you don't want to put your GUIs in PlayerScripts or StarterPlayerScripts - they'll only be rendered if they're under PlayerGui. If you don't want them to be rendered, you can either make them invisible or manually control when they are copied into PlayerGui. For example, you can store them somewhere in ReplicatedStorage and use Clone() to copy them into a Player's PlayerGui when needed.

If you store them in StarterGui, ROBLOX will automatically copy them into each player's PlayerGui either when they join the game, or when they spawn (if StarterGui.ResetPlayerGuiOnSpawn is true).

But what should I DO?

In my current project, I control the whole character and script instantiation process manually - nothing in StarterPlayer or StarterGui. I do this for 2 reasons:

  1. I don't know about the underlying implementation of the ROBLOX engine, so I prefer to control the order in which things happen manually so that it is easier to debug and not susceptible to unannounced changes.
  2. I can guarantee that server-side player data is setup before the clients are allowed to interact with it

ROBLOX doesn't make it easy to control what's going on with much fidelity. It's probably not worth it, so here are some simple rules to follow which will set you off in a healthier direction:

  1. If the script will reset when the player dies, put it in StarterCharacterScripts
  2. If you want the script to continue running whether the player is dead or alive, put it in StarterPlayerScripts
  3. If you want the Player to have a certain GUI (along with scripts that control it) immediately when they join/spawn, put them in StarterGui
  4. Any script or GUI which you don't want the player to have immediately, store in ReplicatedStorage

Have fun experimenting. Maybe one day you'll come up with an amazing structural solution and you can come back here to answer your own question!

Ad
Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
7 years ago

Local scripts will only run in StarterPlayerScripts, PlayerGui, Backpack, and the Character model. That is, basically anything local to an individual player.

To make the local script run every time you respawn, put it in StarterGui or StarterPack. This will ensure that each time you respawn, the local script is cloned into PlayerGui or Backpack respectively, causing it to run again.

If you want the local script to only run once when you join the game, put it in StarterPlayerScripts.

You probably won't put your local scripts in the Character model too often, unless you're doing something specific like animations. But remember it's an option.

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

I suggest using this to get a players name when they join the game:

game.Players.PlayerAdded:connect(function(player)
    print(player)
end)

By the way, put this in ServerScriptService

1
Don't be confused by this TMGOR. AlphaGamer150 is assuming you were using a regular script. Using LocalPlayer for local scripts is the easiest and most effective method. Use PlayerAdded only if it's a server script instead. Troidit 253 — 7y
0
So, I would use this if I wanted to, say.. create a GUI alerting all players that someone has just joined? TMGOR 20 — 7y
0
Yes AlphaGamer150 101 — 7y

Answer this question