Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My math.Random isnt working?

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

storage["Kagune" .. tostring(math.random(8))]:clone().Parent = backpack

I have 8 Different local scripts in Replicated storage this script should put a random 1 in my backpack when I enter the game but it doesnt.

This is in a local script in StarterPack and the 8 local scripts I have are:

Kagune1 Kagune2 Kagune3 Kagune4 Kagune5 Kagune6 Kagune7 Kagune8

There are other things in Replicated Storage such as models.

1 comment

0

It is easier to use the "PlayerGui" then "Backpack"

2 answers

"storage" is a list of children, but you're indexing it with a string key (the name of the script). Get rid of the ":GetChildren()" part on the first line and it should work.

you have to put 2 arguments into math.random().

So if you want 1-8, you would do

math.random(1,8)

so for your code it would become:

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

local kaguneClone = storage["Kagune" .. tostring(math.random(1,8))]:Clone()
kaguneClone.Parent = backpack

I changed the cloning line so it would be easier to read^^

1 comment

0

Wrong. If only one argument is given, math.random automaitcally makes the range from 1 - given arg.

This discussion is preserved as it was. New answers, comments, and votes are closed.