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

I need help with math.random Script first time using Help plz?

Asked by 4 years ago

output: 20:40:25.927 - Workspace.Script (1, 14): Expected identifier, got '='

local Randomizer = {}
Randomizer{1} = "Gun"
Randomizer{2} = "Knife"
Randomizer{3} = "DeathMatch"
print(math.random(1, 3))

2 answers

Log in to vote
1
Answered by 4 years ago

So, two things.

To index a table, along with giving it a value, you use []s, and you're using {}s.

local foo={}
foo[1]="Hello."
foo[2]="I'm in a table"
foo[3]=3+5
--...

Second, I'm guessing you want to print a random value in that table, and you're doing well so far. The thing you're missing is that you're just printing a random value, instead of getting the thing in that position in the table.

In your example, that'd go like so.

local Randomizer = {}
Randomizer{1} = "Gun"
Randomizer{2} = "Knife"
Randomizer{3} = "DeathMatch"
print(Randomizer[math.random(1, 3)])
--Alternatively, you can do something in this style, it's the same result.
local randomNumber=math.random(1, 3)
print(Randomizer[randomNumber])
--Or like so
local randomNumber=math.random(1, #Randomizer) --#Table gives you the length of a table
print(Randomizer[randomNumber])
--...So on
0
oh Vortex_Vasne 89 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Why wont this work?

local Randomizer = {}
Randomizer[1] = "Gun"
Randomizer[2] = "Knife"
Randomizer[3] = "DeathMatch"
print(math.random(1, 3))
if Randomizer == 2 then 
    print("Works All Right!")
    end

Answer this question