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 ) |
04 | end |
05 | if 2 then |
06 | game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(- 37 , 5 , 0 ) |
07 | end |
08 | if 3 then |
09 | game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new( 197 , 5 , 154 ) |
10 | end |
11 | if 4 then |
12 | game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(- 99 , 5 , - 217 ) |
13 | end |
14 | wait( 1 ) |
15 | end |
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
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
.
01 | local i = math.random( 4 ) |
02 |
03 | if i = = 1 then |
04 | game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new( 154.8 , 5 , - 167.8 ) |
05 | end |
06 | if 2 then |
07 | game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(- 37 , 5 , 0 ) |
08 | end |
09 | if 3 then |
10 | game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new( 197 , 5 , 154 ) |
11 | end |
12 | if 4 then |
13 | game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(- 99 , 5 , - 217 ) |
14 | end |
15 | wait( 1 ) |
16 | end |
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;
1 | local a = { 3 , 2 , 1 } |
2 | local b = a [ 1 ] |
3 | print (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.
01 | --Table of positions |
02 | local 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 |
10 | local i = math.random( 1 ,#Positions) |
11 | --Random Position, from 'Positions' table. |
12 | local pos = Positions [ i ] |
13 | --Character Variable |
14 | local char = game.Players.LocalPlayer.Character.Torso |
15 |
16 | --Teleport character |
17 | char.CFrame = pos |
Wouldn't you need to say if i == number
instead of just if number
?