Teleporting specific players

Hello I am making a game that spawns a random map each round, and when a player reaches the end of the map, they get teleported to the center of the lobby.

In the end of the round, I want the players who didn’t complete the map to get teleported to the center of the lobby. However, it is teleporting all players to the center of the lobby when the round ends…

This is the teleport part of the script:

–Teleport back to lobby

for _, Player in pairs(Players:GetChildren())do
	if Player.Character and Player.Character:FindFirstChild('Humanoid') then
		Player.Character.HumanoidRootPart.CFrame = game.Workspace.Lobby.SpawnLocation.CFrame
	end
end

Can someone help me? I just want the players who DIDN’T complete the map to be teleported to the center of the lobby.

I want the players who already completed the map to be able to stay in the corners of the lobby, etc, not the center.

Here is the full script I’m using:

ServerStorage = game:GetService(“ServerStorage”)
ReplicatedStorage = game:GetService(“ReplicatedStorage”)
Players = game:GetService(“Players”)

Maps = ServerStorage:WaitForChild(‘Maps’):GetChildren()
Status = ReplicatedStorage:WaitForChild(‘Status’)

while true do

--Intermission

local Countdown = 10 -- ten second intermission, make this as long as you want

repeat wait(1)
	Countdown = Countdown - 1

	Status.Value = 'Intermission : '..Countdown
until Countdown <= 0

--Choose the map.

Status.Value = 'Choosing Map...'

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

wait(5) -- little pause, make this as long as you want

ChosenMap.Parent = workspace
Status.Value =  'Next Map Is: '

wait(2) -- little pause, make this as long as you want

Status.Value = ChosenMap.Name

wait(2) -- little pause, make this as long as you want

--teleport the players

for _, Player in pairs(Players:GetChildren())do
	if Player.Character and Player.Character:FindFirstChild('Humanoid') then
		Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
	end
end

Countdown = 15 -- Game Time, make this as long as you want.

repeat wait(1)
	Countdown = Countdown - 1

	Status.Value = 'Time left : '..Countdown
until Countdown <= 0

--Teleport back to lobby

for _, Player in pairs(Players:GetChildren())do
	if Player.Character and Player.Character:FindFirstChild('Humanoid') then
		Player.Character.HumanoidRootPart.CFrame = game.Workspace.Lobby.SpawnLocation.CFrame
	end
end

ChosenMap:Destroy()

end

1 Like

I’d recommend using CollectionService. You need to split / organize the players that have completed the map. So propably the easiest way to do that would be making a brick that marks the player as “winner” or something.

Put script in brick, like this

local cs = game:GetService("CollectionService")

script.Parent.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
		cs:AddTag(part.Parent, "Finished")
	end
end)

Now, when a player touches that brick, he is marked. Now you need to change the script to teleport all players that are not marked.
Replace this after the “teleport back to lobby” comment.

for _, Player in pairs(Players:GetChildren())do
	if Player.Character and Player.Character:FindFirstChild('Humanoid') and not cs:HasTag(Player.Character, "Finished") then
		Player.Character.HumanoidRootPart.CFrame = game.Workspace.Lobby.SpawnLocation.CFrame
	end
end

for _, char in pairs(cs:GetTagged("Finished")) do -- This removes all tags, for new map
	cs:RemoveTag(char, "Finished")
end
ChosenMap:Destroy()

Also put local cs = game:GetService("CollectionService") under the players variable.

1 Like

Wow it worked! Thank you so much you made my day XD

1 Like