Telepot each player to a random location

Problem: I wanted the script to teleport each player to a random spawn location but, every single player get’s teleported to the same location that is chosen. How can I teleport each player to a different location?

Script:
local MapChosen = Maps[math.random(1, #Maps)]:Clone()
local SpawnLocations = MapChosen:FindFirstChild(‘Spawns’):GetChildren()
local RandomLocation = Spawns[math.random(1, #Spawns)]

for _, Player in pairs(Players:GetChildren())do
	if Player.Ready.Value == true then
	if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.HumanoidRootPart.CFrame = RandomLocation.CFrame
	end	
	end
end
1 Like

This won’t work unless you have one spawn (AT LEAST) for each player. This isn’t the best way to do this, but oh well here

Feel free to make modifications (I slapped this together lazily lol)

local MapChosen = Maps[math.random(1, #Maps)]:Clone()
local SpawnLocations = MapChosen:FindFirstChild(‘Spawns’):GetChildren()

local function pickspawn()
     local RandomLocation = Spawns[math.random(1, #Spawns)]
     for i,v in pairs(Spawns) do if v == RandomLocation then table.remove(Spawns,i) end
     return RandomLocation
end

for _, Player in pairs(Players:GetChildren())do
        local lol = pickspawn()
	if Player.Ready.Value == true then
	if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.HumanoidRootPart.CFrame = lol.CFrame
	end	
	end
end
1 Like

No problem thank you Ill see what I can do!

1 Like

I tried it out and even changed it a bit but, it would just not even teleport me

For each player to be teleported to a different spawn you would need to randomize the spawn inside the loop, if you put it outside the for loop like in your code, the game would just pick a random spawn, then teleport everyone to that single spawn instead of picking a random spawn for each player

1 Like
local RandomLocation = SpawnLocations[math.random(1, #SpawnLocations)]
--SpawnLocations, not Spawns

Unless if there’s a table, I could be wrong.

1 Like

I might be wrong, but you would simply put local RandomLocation = Spawns[math.random(1, #Spawns)] inside the loop.

local MapChosen = Maps[math.random(1, #Maps)]:Clone()
local SpawnLocations = MapChosen:FindFirstChild('Spawns'):GetChildren()

for _, Player in pairs(Players:GetChildren())do
    local RandomLocation = Spawns[math.random(1, #Spawns)]

	if Player.Ready.Value == true then
	if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.HumanoidRootPart.CFrame = RandomLocation.CFrame
	end	
	end
end
1 Like

Are you getting any errors or is it just not teleporting you?

No erros just not teleporting me when I use the script that @Unphases provided

for _, Player in pairs(Players:GetChildren())do
	if Player.Ready.Value == true then
    local RandomLocation = Spawns[math.random(1, #Spawns)]
	if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.HumanoidRootPart.CFrame = RandomLocation.Position
	end	
	end
end

Summary: Put the local RandomLocation = Spawns[math.random(1, #Spawns)] code in the for loop

2 Likes

ahh yes! thank you this worked like a charm :slight_smile:

You also Gave me the solution if I could give two solutions I would but sadly, no :smile: