I want so it teleports to one of the 3 positions.
local Plr = script.Parent.Parent.Parent.Parent.Parent.Parent local Btn = script.Parent local Po1 = Vector3.new(-223.944, 1.212, -45.098) local Po2 = Vector3.new(-5.944, 1.212, -45.098) local Po3 = Vector3.new(-223.944, 555.212, -455.098) local Gui = Btn.Parent.Parent.Parent function Click() Plr.Character:MoveTo(Po1 or Po2 or Po3) Gui:remove() end Btn.MouseButton1Click:connect(Click)
local Plr = script.Parent.Parent.Parent.Parent.Parent.Parent local Btn = script.Parent local Po1 = Vector3.new(-223.944, 1.212, -45.098) local Po2 = Vector3.new(-5.944, 1.212, -45.098) local Po3 = Vector3.new(-223.944, 555.212, -455.098) local Gui = Btn.Parent.Parent.Parent function Click() local Num = math.random(1,3) if Num == 1 then Plr.Character:MoveTo(Po1) elseif Num == 2 then Plr.Character:MoveTo(Po2) elseif Num == 3 then Plr.Character:MoveTo(Po3) end Gui:remove() end Btn.MouseButton1Click:connect(Click)
Place your possible positions in a table and then use math.random
to select an index from that table.
possiblePositions = {Po1, Po2, Po3} --... Plr.Character:MoveTo(possiblePositions[math.random(#possiblePositions)])
Hello there!
Since it seems you wish the randomize the position, you can use a useful tool lua has prebuilt! Here is a great link for a more in depth explanation:http://wiki.roblox.com/index.php?title=Random_numbers
To begin you have a great start, but when moving the player, don't use or statements, that basically saying,
Plr.Character:MoveTo(true)
because you are checking if a variable is existent.
So, how about we clean up the function method to allow a random Position to be choosen?
function Click() local randomNumber = math.random(1, 3) end
The first step we just did, was allow the game to choose a random number from 1 - 3. This will allow use to select the corresponding position in a second.
Since it will be much easier to work with, we're going to rewrite the positions, in a table.
local positionList = { Vector3.new(-223.944, 1.212, -45.098), Vector3.new(-5.944, 1.212, -45.098), Vector3.new(-223.944, 555.212, -455.098) }
There we go! Now we can easily use this is the next piece of code, which gives us a position to use.
function Click() local randomNumber = math.random(1, 3) local position = positionList[randomNumber] end
If you don't know already, the randomNumber is indexing that specific spot in the list, so if it was 1, it will return the first object in the list.
You may think it is finished, but this will actually always give you the same result, when creating a new roblox server. To fix this, we can use a way to set the seed for the randomizer. Take a look below.
function Click() math.randomseed(os.time()) local randomNumber = math.random(1, 3) local position = positionList[randomNumber] end
Basically, the math.randomseed() sets the seed based on what's inside the parameter.
Now let's retype this code into the full script, so you get a better visual to how this works together!
local plr = script.Parent.Parent.Parent.Parent.Parent.Parent local btn = script.Parent local gui = script.Parent.Parent.Parent local positionList = { Vector3.new(-223.944, 1.212, -45.098), Vector3.new(-5.944, 1.212, -45.098), Vector3.new(-223.944, 555.212, -455.098) } function click() math.randomseed(os.time()) local randomNumber = math.random(1, 3) local position = positionList[randomNumber] end btn.MouseButton1Down:connect(click)
If you see an error, tell me. Me fixing the error will help you and other's who may use this question as a reference!