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 = CFrame.new(game.Workspace.SchoolMap.TeleportPart.Position + Vector3.new(xPos, 0, zPos))
end
Would recommend using :GetPlayers() instead of :GetChildren()
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 + Vector3.new(xPos, 0, zPos)
plr.Character:MoveTo(newPosition)
end
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 = Random.new() -- 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.