game.Players.PlayerAdded:connect(function(player) local Rep = game.ReplicatedStorage math.randomseed(tick()) for index = 1, 8 do local Kag = Rep(math.random()["Kagune" .. index]) Kag.Parent = game.Players.LocalPlayer.Starterpack end end)
This script should choose one of the 8 Local Scripts in Replicated Storage and put it in my StarterPack. However it isnt working and the output isnt showing any errors. It is a Normal Script in Workspace.
I just added a random tag.
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:
game.Players.PlayerAdded:connect(function(player)
to:
game.Players.ChildAdded:connect(function(player)
Also be aware that this will trigger if you parent non-player instances to Players
.
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).
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.
storage = game.ReplicatedStorage --only need to reference storage once backpack = game.Players.LocalPlayer.Backpack --only need to reference once math.randomseed(tick()) -- only need to seed once game.Players.ChildAdded:connect(function(player) local kag = storage["Kagune" .. tostring(math.random(8))]:clone() kag.Parent = backpack end)
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:
game.Players.ChildAdded:connect(function(player) storage["Kagune" .. tostring(math.random(8))]:clone().Parent = backpack end)
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.
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:
storage = game.ReplicatedStorage backpack = game.Players.LocalPlayer.Backpack math.randomseed(tick()) storage["Kagune" .. tostring(math.random(8))]:clone().Parent = backpack
In line 10 you have a mistake it should be "Backpack" instead of Starterpack
, but right now I will be using PlayerGui
Here is the Code:
game.Players.PlayerAdded:connect(function(plr)--use a script in order for this to work local rep=game.ReplicatedStorage:GetChildren() local keg=rep[math.random(1, #rep)] keg.Parent=plr:findFirstChild("PlayerGui") end)
If you have other stuff in The ReplicatedStorage, you might want to use this
CODE2:
game.Players.PlayerAdded:connect(function(plr)--use a script in order for this to work local rep=game.ReplicatedStorage.MODRLorPARENT:GetChildren() local keg=rep[math.random(1, #rep)] keg.Parent=plr:findFirstChild("PlayerGui") end)