Hello, I made a script that I’d like to share. What does this script do? Well, it changes the position of players easily so you can start working on more improtant aspects of your game.
Step 1:
Create a Server Script in Server Script Service
Step 2:
Paste this Code into the Script:
local function Teleport(player, location, multiple_positions_triggered)
if multiple_positions_triggered then
if list then
list = nil
set_bounce = nil
end
end
if list then
set_bounce = true
end
if not player:IsA("Player") then
return print("Could not find player. Make sure you set the player variable to a player instance.")
end
if script:FindFirstChild(location) then
random = script[location]:GetDescendants()[math.random(1,#script[location]:GetDescendants())]
part = random
else
return print("Could not find folder name specified.")
end
local function Set_Position()
if retry then
part = script[location]:GetDescendants()[math.random(1,#script[location]:GetDescendants())]
end
minX,minZ = math.random(part.Position.X - part.Size.X / 2, part.Position.X + part.Size.X / 2)
,math.random(part.Position.Z - part.Size.Z / 2, part.Position.Z + part.Size.Z / 2)
if list then
for i,v in pairs(list) do
if minX == v[1] and minZ == v[3] then
retry = true
end
end
end
retry = false
workspace[player.Name]:MoveTo(Vector3.new(minX,part.Position.Y + 2.5,minZ))
if not multiple_positions_triggered and not set_bounce then
list = {}
list[1] = {minX,part.Position.Y + 2.5,minZ}
end
end
if workspace:FindFirstChild(player.Name) then
Set_Position()
else
workspace:WaitForChild(player.Name)
Set_Position()
end
if retry then
retries = 0
while retry do
Set_Position()
task.wait()
retries += 1
if retries >= 60 then
return print("Too many retries have occured.")
end
end
end
end
game.Players.PlayerAdded:Connect(function(player)
Teleport(player, "Lobby", false)
end)
Step 3: Create a folder inside the script called “Lobby”. Add a Part inside Workspace and then place the Part inside the Lobby Folder. Each player that joins will spawn on that part.
How it Works: To call the function, you need to type in Teleport(arg1, arg2, arg3). Arg1 is the player your teleporting like: game.Players[your_username]. Arg2 is the folder inside the script you want to pick, it will choose a random part inside that folder, get its position and spawn the player to a random position on the part. Arg3 is either false, nil, true or you can not put anything. If not true, it won’t spawn players in the same position as before. This will prevent players from stacking up on each other. If you want to stack players on each other but already called the function with Arg3 being false or nil, then the next time you do it you have to set Arg3 to true.
Edit 1: Made some changes that improved the function and removed a bug that would crash the server