PlayerAdded
The description for Players.PlayerAdded
on the Official ROBLOX Wiki reads:
"Fires when a player enters the game. This event does not fire for LocalScripts - use ChildAdded instead."
Hence, you need to change the first line from:
1 | game.Players.PlayerAdded:connect( function (player) |
to:
1 | game.Players.ChildAdded:connect( function (player) |
Also be aware that this will trigger if you parent non-player instances to Players
.
Choosing Random Children
At the moment, line 8 is semantically incoherent (meaningless) because you're treating game.ReplicatedStorage
as a function. When you want to index (get the value of) a certain key from a table, you use square brackets [
and ]
, not regular parentheses (
and )
.
Secondly, you don't need a for loop that goes through each of the 8 possible values in order to select one of them. math.random
will choose an index from the range that you specify and you only need that one value to choose a single script.
Thirdly, in some cases you can't index the children of an instance by directly using the square bracket syntax, you need to get a table of the instance's children by calling the :GetChildren()
method on that instance, or otherwise constructing a table of possible objects.
Now, depending on what other things you have in ReplicatedStorage
, you may be able to use the shortcut that TheContinentOfEurope uses in his answer. (rep[math.random(1,#rep)]
). If the only instances in your ReplicatedStorage
are the possible scripts that you want to choose, then use that shortcut. Otherwise (if you have any other things in there), you will need to specify which set of things to choose from (which is what you were doing originally).
Other Corrections
Don't forget to clone()
. If you change the parent of the instance that you want from ReplicatedStorage
to Player.Backpack
, it will no longer be in the storage. You'll want to copy (clone()
) the instance so that, when the player re-spawns, all the scripts will still be there to choose from.
Lastly, Player
s don't have Starterpack
s, they have Backpack
s. game.Starterpack
is the template for making new Backpack
instances, whilst Backpack
is the actual container for the Player
's belongings.
New Script
1 | storage = game.ReplicatedStorage |
2 | backpack = game.Players.LocalPlayer.Backpack |
5 | game.Players.ChildAdded:connect( function (player) |
6 | local kag = storage [ "Kagune" .. tostring (math.random( 8 )) ] :clone() |
If you don't need to perform any further manipulation on the cloned script instance, besides setting the parent, then you can actually shorten the body of the function to a single line, like-so:
1 | game.Players.ChildAdded:connect( function (player) |
2 | storage [ "Kagune" .. tostring (math.random( 8 )) ] :clone().Parent = backpack |
As you can see, there were quite a few major and minor problems with your initial script. Hence, as I haven't tested what I've written, there may be slight mistakes still left. If you find any, please point them out. Otherwise, feel free to comment follow-up questions if you'd like something explained in more detail.
Edit:
In hindsight, I realise that you don't even need to be connecting to the ChildAdded
or PlayerAdded
events if you just want to load one of those scripts when the player joins.
Your LocalScript
only needs to do this:
1 | storage = game.ReplicatedStorage |
2 | backpack = game.Players.LocalPlayer.Backpack |
5 | storage [ "Kagune" .. tostring (math.random( 8 )) ] :clone().Parent = backpack |