I’m having trouble figuring out how to teleport players to any random place on the map.
local player = game.Players.LocalPlayer script.Parent.Touched:Connect(function(part)) end
Do I put math.randomseed(tick()) or (os.time()) instead of tick. What do they do and what’s the difference? Also what is the difference between Vector3.new, Vector3, CFrame, and CFrame.new compared to each other?
"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:
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.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):
math.randomseed(tick()) math.random() -- throw out first not-very-random value (known limitation of lua)
Example for #2: Say you put all your parts in workspace.SpawnLocations
(a folder), you could choose one of them at random like this:
local ch = workspace.SpawnLocations:GetChildren() local pos = ch[math.random(1, #ch)].Position
Example for #3:
local positions = { Vector3.new(0, 10, 0), Vector3.new(15, 15, 100), --etc } local pos = positions[math.random(1, #positions)]
Quick explanation, Vector3 is postioning it can also be used as a table or an array of three numbers. Cframe is for moving things basically taking any attached parts with it and position them to their position before they moved.
Also, if you're doing this in a 'LocalScript', you're best not to do this (script.Parent.Touched:Connect(function(part))).
math.randomseed is automatically set.
v So this is what I have done.(make sure this is a normal script)
script.Parent.Touched:connect(function(part) local spawnpoints = math.random(1,3) if spawnpoints == 1 then if part.Parent:FindFirstChild('Humanoid') then part.CFrame = workspace.Part1.CFrame end end if spawnpoints == 2 then if part.Parent:FindFirstChild('Humanoid') then part.CFrame = workspace.Part2.CFrame end end if spawnpoints == 3 then if part.Parent:FindFirstChild('Humanoid') then part.CFrame = workspace.Part3.CFrame end end end)