Script to teleport all players random

How could I make a server script thats makes all the players teleport to a random position around a part I tried this, but it din’t work.

for _, plr in pairs(game.Players:GetChildren()) do
			local xPos = math.random(-10,10)
			local zPos = math.random(-10,10)
			plr.Character.HumanoidRootPart.CFrame = +, 0, zPos))
1 Like

Try doing plr.Character.PrimaryPart instead of HumanoidRootPart

1 Like
  1. Would recommend using :GetPlayers() instead of :GetChildren()

  2. Would recommend using :MoveTo() which is under plr.Character which can help easen up the process of relocating a player.

for _, plr in pairs(game.Players:GetPlayers()) do
	local xPos = math.random(-10, 10)
	local zPos = math.random(-10, 10)
	local newPosition = game.Workspace.SchoolMap.TeleportPart.Position +, 0, zPos)
1 Like

Where is this Script Located? and how exactly are you firing this script when it comes to usage?

Here is a couple of other things:

When wanting to teleport players, its recommended you use :PivotTo() depending if you want to set both Position, and Orientation using CFrame, otherwise you can use :MoveTo() for just Position, which uses Vector3, Setting the CFrame of the HumanoidRootPart would be similar to using :PivotTo(), so if you are looking for the exact same usage, stick with that one.

If you want “completely” random Results, use Random instead of math.random, if you want to include Decimals, use Random:NextNumber(), which will give you a number with Decimals, however for use case with math.random, you can give it no arguments to work, and it will result in it just return a decimal between 0 and 1, which you can Multiply by 10:

local random = -- grabbing the Random Instance

offX1 = math.random() * 10 -- random decimal [0 - 1] * 10 to be within the one's place
offX2 = random:NextNumber(0, 10) -- random decimal [0 - 10]

I would go with Random with you are looking for fair usage of numbers.