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

Why isn't my math.random script working? Am i using the wrong 'math' ?

Asked by 8 years ago
Edited 7 years ago

Ok, i have attempted multiple times with different things but they don't seem to work. My current attempt looks like this

local rare = math.random(2)
while true do
    wait(1)
if rare == 1 then
    game.Workspace.KeemTest.BrickColor = BrickColor.White()
    print("1")
elseif rare == 2 then
    print("2")
    game.Workspace.KeemTest.BrickColor = BrickColor.Black()
else
    print("Other Number")
end
end

i haven't been able to figure it out and im confused, can anyone help me?

2 answers

Log in to vote
0
Answered by 8 years ago

What you're doing

Right now, you're creating one random number. The value of rare doesn't change since you only called the math.random function once.

local rare = math.random(2)
-- Let's say that, randomly, it has a value of 2.
while true do
    print(rare)
end
--[[
> 2
> 2
> 2
> 2
...
--]]

What you want to do

If you want to create a new random number on every loop iteration, call the math.random function inside the loop.

while true do
    local rare = math.random(2)
    -- Rest of your code follows.
end

Asking good questions

From reading your question, it's not clear what you're "attempting." Saying things like "I have attempted multiple times with different things but they don't seem to work" and "i haven't been able to figure it out" doesn't explain what you're trying to accomplish. Your question doesn't explain what "it" is.

Also, read through this: How to Post Good Questions and Answers

Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

It might work if you add an parameter to math.random().

math.random(start, end) would be like doing math.random(1, 10) and picking a random (not really random, see here.) number between 1 and 10.

local rare = math.random(1, 2)

Also, you're not using the wrong math. ;)

Answer this question