Teleport players to spesific part randomly [HELP]

How can i make players teleport to spesific part when timer ends?
It will not be all players to one part randomly, each player will be one position randomly.
like player 1 can go blue or player 2 can go pink but all will be on diffrent part.
Can somebody help?

6 Likes

You could setup a for loop that Teleports the player to the next spawn location

3 Likes

Yea it would be a loop but i don’t know how to make that teleport thing…

4 Likes

If you want it to be purely random, use math.random to index the part, then remove it from a table containing all the posible spawns. Here’s a simple function, set it up with your own code:

local function TeleportPlayers()
	local Spawns = SpawnsFolder:GetChildren()

	for _, player in ipairs(Players:GetPlayers()) do
		local RandomSpawn = Spawns[math.random(1, #Spawns)]
		
		table.remove(Spawns, table.find(Spawns, RandomSpawn))
		player.Character:MoveTo(RandomSpawn.Position)
	end
end
4 Likes

you can create an empty table, another table with all of the spawn parts and then make a loop for each player, in the loop define the random spawnpart so like


for i,v in charactersFolder:GetChildren() do
  local randomPart 
  
  repeat
    randomPart = spawnPartsTable[math.random(1,#spawnPartsTable)]
    task.wait()
  until not table.find(alreadyPickedParts,randomPart)
  
  table.insert(alreadyPickedParts,randomPart)
  char.PrimaryPart.CFrame = randomPart.CFrame * CFrame.new(0,3,0)
end

thats it, it should work unless i made some mistakes try it out

4 Likes

I just made it like that but it didn’t work, sorry i am bad at scriptting, is that wrong? I made script in serverscriptservice

local Players = game:GetService("Players")
local SpawnsFolder = game.Workspace.Holders

local function TeleportPlayers()
	local Spawns = SpawnsFolder:GetChildren()

	for _, player in ipairs(Players:GetPlayers()) do
		local RandomSpawn = Spawns[math.random(1, #Spawns)]

		table.remove(Spawns, table.find(Spawns, RandomSpawn))
		player.Character:MoveTo(RandomSpawn.Position)
	end
end
2 Likes

you need to call the functions too

local Players = game:GetService("Players")
local SpawnsFolder = game.Workspace.Holders

local function TeleportPlayers()
	local Spawns = SpawnsFolder:GetChildren()

	for _, player in ipairs(Players:GetPlayers()) do
		local RandomSpawn = Spawns[math.random(1, #Spawns)]

		table.remove(Spawns, table.find(Spawns, RandomSpawn))
		player.Character:MoveTo(RandomSpawn.Position)
	end
end

TeleportPlayers()

do that

2 Likes

It didn’t teleported me to parts too… Am i missing something?

1 Like

are you sure? did you get any errors or something, it should work

1 Like

Not even one error. I don’t know why.

2 Likes
local players = game:GetService("Players")
local spawnPartsTable = game.Workspace.Holders:GetChildren()
local alreadyPickedParts = {}


for i,v in players:GetPlayers(plr) do
  local character = plr.Character
  local randomPart 
  
  repeat
    randomPart = spawnPartsTable[math.random(1,#spawnPartsTable)]
    task.wait()
  until not table.find(alreadyPickedParts,randomPart)
  
  table.insert(alreadyPickedParts,randomPart)
  character.PrimaryPart.CFrame = randomPart.CFrame * CFrame.new(0,3,0)
end

try my version out

2 Likes

You’re probably calling the function before any player has loaded in. Try using a wait, or a loop to wait for a client to join before you call the function:

repeat task.wait(1) until #Players:GetPlayers() >= 1
3 Likes

This one didn’t helped sir. No Error no Teleport, maybe he is right, it try teleport before char loaded.

1 Like

Did you call the function somewhere in the script?

2 Likes

yeah add the code he provided and both versions should work, if you want it to wait until a certain amount of players are ingame just change the 1 to whatever number of players you want

2 Likes

To add on, you’ll probably have to wait for the character to load too, you can create a variable and use CharacterAdded to wait for it.

local Character = player.Character or player.CharacterAdded:Wait()

All of this isn’t really necessary if you implement the function to your system though.

1 Like

Okay now i made thoose after ride your all messages

local Players = game:GetService("Players")
local SpawnsFolder = game.Workspace.Holders

local function TeleportPlayers()
	local Spawns = SpawnsFolder:GetChildren()
	repeat task.wait(1) 
	for _, player in ipairs(Players:GetPlayers()) do
		local RandomSpawn = Spawns[math.random(1, #Spawns)]

		table.remove(Spawns, table.find(Spawns, RandomSpawn))
		player.Character:MoveTo(RandomSpawn.Position)
		
		end 
	until #Players:GetPlayers() >= 1
end

TeleportPlayers()

and i get this error now.

ServerScriptService.Teleport:11: attempt to index nil with ‘MoveTo’ - Server - Teleport:11

As I said, this happens because the script thinks the character doesn’t exist (because it hasn’t loaded yet). Replace the error line by using the variable I provided you in the above post.

1 Like

Thank you, and all for helping it works now!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.