Answered by
7 years ago Edited 7 years ago
"Do I put math.randomseed(tick()) or (os.time()) instead of tick. What do they do and what’s the difference?"
Computers can't generate truly random numbers, but the closest thing (which is perfectly fine for games) is pseudo-random generation. This requires a "seed" which you provide by calling math.randomseed
. If you provide the same seed every time, you'll get the same series of random numbers when you call math.random
. Since tick()
(or os.time()
) are always changing (roughly speaking), they are good things to pass into math.randomseed
. (You can look up the difference between these two things, but they both return the current time as a very large number.)
"Also what is the difference between Vector3.new, Vector3, CFrame, and CFrame.new compared to each other?"
Vector3
is the class, Vector3.new
is the function you need to create a new one. Vector3 represents a 3D position; it stores 3 numbers, X, Y, and Z. These are used to store the position of a Part, for instance. CFrame
stores not only a position but also an orientation (ex not just where something is, but also how it's rotated). When moving things around in your game, modifying .Position (which takes in a Vector3) will usually put the thing you teleported above anything it would otherwise collide with, whereas changing .CFrame will always exactly where you asked. ex, say you try to teleport someone into a chair, .Position will put the player above the chair, .CFrame will just put them inside the chair.
Now, based on your question, your objective is to generate a random Vector3 to move the player's character to. You have various options:
- Generate the Vector3 with math.random:
Vector3.new(math.random(-100, 100), 10, math.random(-100, 100))
(note: assumes that you can always spawn at y=10. If your map is not completely flat, the correct thing to do would be to raycast to determine the required height at a given x/z). This generates a Vector3 anywhere from -100, 10, -100 to 100, 10, 100.
- Manually place a series of parts and randomly choose one of them to teleport to. Example below.
- Store a list of Vector3s in a list and randomly choose one of them. Example below.
For all cases, initiate the random generator like this (in a single script -- if you call randomseed
multiple times you're less likely to get a good random sequence):
Example for #2: Say you put all your parts in workspace.SpawnLocations
(a folder), you could choose one of them at random like this:
1 | local ch = workspace.SpawnLocations:GetChildren() |
2 | local pos = ch [ math.random( 1 , #ch) ] .Position |
Example for #3:
3 | Vector 3. new( 15 , 15 , 100 ), |
6 | local pos = positions [ math.random( 1 , #positions) ] |