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

How to choose a random number?

Asked by
xJesss 5
9 years ago

No- I'm not really asking for something that basic.

Basically, what I'm trying to do is when the player clicks the button, it determines a random number (currently between 1 and 4, though I want to add more) and if the number is 1, it teleports them one place, if its 2 then it teleports them to another place, and so on.

01    local i = math.random(4)
02 if 1 then
03    game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(154.8, 5, -167.8)
04end
05if 2 then
06    game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(-37, 5, 0)
07end
08if 3 then
09    game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(197, 5, 154)
10end
11if 4 then
12    game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(-99, 5, -217)
13end
14    wait(1)
15end

What (i think) the problem is is that the game reads that i = 4 instead of whatever random number it is, but I dont know how to fix it... it always teleports me to option 4

Help please :o

2 answers

Log in to vote
4
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Your Problem

You're not comparing the random number, i, to the specified number! You're just stating if [number] then when you need to state if i == [number] then.

01local i = math.random(4)
02 
03 if i == 1 then
04    game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(154.8, 5, -167.8)
05end
06if 2 then
07    game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(-37, 5, 0)
08end
09if 3 then
10    game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(197, 5, 154)
11end
12if 4 then
13    game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(-99, 5, -217)
14end
15    wait(1)
16end

Efficiency Fixes

Current Code Setup;

  • You should use elseif statements after your initial if statement.

  • You should make a variable, so you don't have to repetitively type game.Players.LocalPlayer.Character.Torso.

New Setup;

But I mostly suggest making a table full of all of the wanted CFrames. This way, you can index the table with the random number..

Indexing a table works like this;

1local a = {3,2,1}
2local b = a[1]
3print(b) -->3

'b' printed '3' because index 1 of table 'a' was 3. So if you index the table randomely then it will get a random index..

Another thing you should know is that the # operator gets the length of any string or table, so if you use math.random with the second argument of #[table], '[table]' being whatever table you're getting the index of, then it will automatically adjust to th table's size.

Goulified Code

01--Table of positions
02local Positions = {
03    CFrame.new(154.8, 5, -167.8),
04    CFrame.new(-37, 5, 0),
05    CFrame.new(197, 5, 154),
06    CFrame.new(-99, 5, -217)
07}
08 
09--Random index number
10local i = math.random(1,#Positions)
11--Random Position, from 'Positions' table.
12local pos = Positions[i]
13--Character Variable
14local char = game.Players.LocalPlayer.Character.Torso
15 
16--Teleport character
17char.CFrame = pos
0
Thank you!! I tried putting i = 1 at first but it wouldnt allow it, I didn't know that it had to have to ==! thank you sooo much :D xJesss 5 — 9y
Ad
Log in to vote
0
Answered by
funyun 958 Moderation Voter
9 years ago

Wouldn't you need to say if i == number instead of just if number?

Answer this question