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=
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.
As you may have discovered on the API page for LocalScript, they will only run in 4 specific locations:
There are 2 differences between scripts in PlayerScripts
and in Character
:
PlayerScripts
are not visible to the server, but they are visible in Character
PlayerScripts
are not reset when the player dies/re-spawns, but they are reset if parented to Character
because the model is usually destroyed.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
).
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:
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:
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!
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.
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