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

Help me with my math.random please?

Asked by
neoG457 315 Moderation Voter
9 years ago
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.

2
why is it StarterPack, should be "Backpack" woodengop 1134 — 9y

2 answers

Log in to vote
2
Answered by
duckwit 1404 Moderation Voter
9 years ago

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:

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.

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, Players don't have Starterpacks, they have Backpacks. game.Starterpack is the template for making new Backpack instances, whilst Backpack is the actual container for the Player's belongings.

New Script

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.

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:

storage = game.ReplicatedStorage
backpack = game.Players.LocalPlayer.Backpack
math.randomseed(tick())

storage["Kagune" .. tostring(math.random(8))]:clone().Parent = backpack
1
+1 woodengop 1134 — 9y
0
The local scripts contain OnKeyDown functions and I shoul be able to use the random local script when I enter the server but there has been no change. There are other models in Replicated storage and the 8 local scripts are called "Kagune 1" "Kagune2" ect neoG457 315 — 9y
1
neoG457: If you just want to load these scripts for the player, you shouldn't be using the ChildAdded/PlayerAdded event anyway. Let me edit my answer to reflect this. duckwit 1404 — 9y
1
Also, NeoG457: Do your script names use the format "Kagune#" or "Kagune #" - i.e. is there a space in there? It matters. If there is a space, you'll need to add one in to the code I put. duckwit 1404 — 9y
View all comments (12 more)
0
No spaces neoG457 315 — 9y
0
So do I put this in a Normal Script Or A Local Script neoG457 315 — 9y
1
It should go in a LocalScript, because we use Players.LocalPlayer to get a reference to the player. duckwit 1404 — 9y
0
Okay. neoG457 315 — 9y
0
It says "Players.neoG457.Backpack.LocalScript:5: attempt to index field "?" (a nil value) neoG457 315 — 9y
1
My mistake. In this case, change storage = game.ReplicatedStorage:GetChildren() to just storage = game.ReplicatedStorage duckwit 1404 — 9y
0
It seens to work however it is only choosing 1 local script. I even tested it on alternative accounts several times but it gave me the same. Are there any ways to give a probability for each local script? Like a 75% chance of getting Kagune4 neoG457 315 — 9y
1
The script works. Are you sure you have actually created different scripts (i.e. is Kagune3 actually different to all the other ones)? There are ways to skew the probability, but math.random is uniform by default. You should post that as a separate question on Scripting Helpers, though. duckwit 1404 — 9y
0
They are all different apart from the OnKeydown functions in them neoG457 315 — 9y
1
And are you are sure that the script I described in my answer selects the same "Kagune" every time the player spawns? How have you tested this? duckwit 1404 — 9y
0
Joining the server let me try again neoG457 315 — 9y
0
Never mind I was wrong, thank you so much for all your help. neoG457 315 — 9y
Ad
Log in to vote
1
Answered by
woodengop 1134 Moderation Voter
9 years ago

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)
2
math.random(1,#rep) is the same as math.random(#rep) duckwit 1404 — 9y
2
Also note that PlayerAdded does not work in LocalScript instances. duckwit 1404 — 9y
1
ik woodengop 1134 — 9y

Answer this question