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!
3 comments
You could also use a table and create a loop that goes through everything in the table and creates a random number between 1 and the number of items in the table and do it that way
This doesn't generalise well. If he adds a 4th position, or a 27th position, he'd have to copy those lines and it would be a redundant pain.
I was only doing that because that is what he asked for, i also posted he could use a table... Thanks for the -1 .-.