Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to teleport players randomly?

Asked by 6 years ago

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?

1
Lemme ask. D'you want it to be a completely RANDOM place in the map, or one of a preset place that you has stored before? Aquaventurer 26 — 6y
0
uhh could you show me how to do both please Subaqueously 11 — 6y
0
os.time does not measure milliseconds, and tick() does. Your choice. hiimgoodpack 2009 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 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:

  1. 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.
  2. Manually place a series of parts and randomly choose one of them to teleport to. Example below.
  3. 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):

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)]
0
Thank you, this helped a lot. However, is there a method that teleports the player to any random spot within a boundary without having to choose from random numbers? Perhaps a function? Or do we have to choose teleport them “randomly” using a random position from a part or something similar? Subaqueously 11 — 6y
0
Sorry, this is really difficult to explain. What I mean is without having to choose from something we tell it to like a parts position or x or -x Subaqueously 11 — 6y
0
Also what is raycasting Subaqueously 11 — 6y
0
I believe you are describing option #1 (which I edited to try and make more clear). math.random(-100, 100) generates a random number between (and including) the two numbers provided. Raycasting info on wiki: http://wiki.roblox.com/index.php/Raycasting chess123mate 5873 — 6y
Ad
Log in to vote
-1
Answered by
xEiffel 280 Moderation Voter
6 years ago

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)
0
If you want to know how to make it to a random setpoint in the whole entire map, just tell me. (Also, my bad on my explanation, I put CFrame as Cframe.) xEiffel 280 — 6y
0
Any place in the map is what I’m looking for but idk how to do that. Also thanks for the explanation and it’s fine it’s just a small typo Subaqueously 11 — 6y
0
You shouldn't store non-position data in vector3s. It just does not make sense why you are storing them in there, and using .X, .Y, and .Z to get non-position values. hiimgoodpack 2009 — 6y

Answer this question