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.
local i = math.random(4) if 1 then game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(154.8, 5, -167.8) end if 2 then game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(-37, 5, 0) end if 3 then game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(197, 5, 154) end if 4 then game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(-99, 5, -217) end wait(1) 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
.
local i = math.random(4) if i == 1 then game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(154.8, 5, -167.8) end if 2 then game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(-37, 5, 0) end if 3 then game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(197, 5, 154) end if 4 then game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(-99, 5, -217) end wait(1) 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;
local a = {3,2,1} local b = a[1] 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.
--Table of positions local Positions = { CFrame.new(154.8, 5, -167.8), CFrame.new(-37, 5, 0), CFrame.new(197, 5, 154), CFrame.new(-99, 5, -217) } --Random index number local i = math.random(1,#Positions) --Random Position, from 'Positions' table. local pos = Positions[i] --Character Variable local char = game.Players.LocalPlayer.Character.Torso --Teleport character char.CFrame = pos
Wouldn't you need to say if i == number
instead of just if number
?