Hello, so I’m making a game kind of like Obby King, but I need help teleporting all players to a random selected area.
make sure that the are have parts that will receive the players(Teleport)
you can try this:
local function Teleport(Players, Spawn) if Players then if Players.Character then local HumanoidRootPart = Players.Character.HumanoidRootPart HumanoidRootPart.CFrame = Spawn[math.random(1,#Spawn)].CFrame + Vector3.new(0,5,0) end end end
Okay, but what would “Spawn” be, then?
You need to put all the teleporters in one folder and name them, preferrably in numbers ex: 1,2,3etcetc
First you put all the Players’ character in a folder to represent that they’re going to play the game.
Secondly you iterate through that folder and change the position of the Character’s humanoidrootpart to that specific teleporter.
wait(5) -- Consider this a sort of intermission or something like that --Parenting Characters to Folder for i,plr in pairs(game.Players:GetPlayers())do plr.Character.Parent = game.Workspace.YourFolder end --Teleporting the characters local totalTPs = #game.Workspace.TeleportersFolder:GetChildren() local ChosenTeleporter = math.random(1, totalTPs) for i,character in pairs(game.Workspace.YourFolder:GetChildren()) do character.HumanoidRootPart.Position = game.Workspace.TeleportersFolder[tostring(ChosenTeleporter)].Position end
Im going to try this out quickly as I wrote it off the top of my head.
no need for specific names you can just use Instance:GetChildren to get all the instances inside the folder and select a random one from there.
local Parts = workspace.Parts:GetChildren() local SelectedPart = Parts[math.random(1, #Parts)] for _, Player in next, game:GetService("Players"):GetPlayers() do Player.Character:MoveTo(SelectedPart.Position) end
Simply create a container inside workspace containing the parts you want to teleport players to. (in the example it’s name Parts)
Isn’t that what I did though? If I got you correctly, you are referring to choosing the random teleporter?
Instead of shortening it by using a variable I wrote it in full?
Ah. I seem to have misread it by glancing over it. Sorry!
Hey, just to let you know that I edited the code a bit and it should work fine as I tested it.
Here’s a small reason why you should use Random.new over math.random, in case you’d like to read.
I personally don’t agree with the reasons given by the person except for the fact that
math.random is shared between all scripts, if you don’t need repeatable results that doesn’t matter. The overhead part is practically meaningless in this day and age where computers can execute millions of instructions a second. I would almost say they’re even incorrect as since math.random is calling the random implementation from the C side you don’t have to invoke
Random.new which will take up more time being instanced as it has to be registered with the lua gc and such. It also 2 separate calls (one to Random.new and one to the next function)