Teleporting players to their own spawns

Hello! I’m wondering how I can make a script where all players get teleported to their own spawn.

2 Likes

local Players = game:GetService("Players")

for i,v in pairs(Players:GetPlayers()) do

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

if Character ~= nil then

Character:WaitForChild("HumanoidRootPart").CFrame = workspace.Spawns[Character.Name].CFrame -- // [Character.Name]= their spawn?

end
end

If you’re working with teams then the following will work.

local Teams = game:GetService("Teams")

local function backToTeamSpawn()
	for _, Team in ipairs(Teams:GetTeams()) do
		for _, TeamPlayer in ipairs(Team:GetPlayers()) do
			local TeamCharacter = TeamPlayer.Character
			if TeamCharacter then
				for _, TeamSpawn in ipairs(workspace:GetDescendants()) do
					if TeamSpawn:IsA("SpawnLocation") then
						if TeamSpawn.TeamColor.Name == Team.TeamColor.Name then
							TeamCharacter:PivotTo(TeamSpawn.CFrame * CFrame.new(0, 3, 0) * CFrame.Angles(0, math.rad(math.random(0, 360)), 0))
						end
					end
				end
			end
		end
	end
end

No, I’m trying to make it so like (you know my post on how the script only works once which you just replied to?) so they spawn on one of those pillars that is not occupied by somebody.

local Players = game:GetService("Players")
local Server = game:GetService("ServerStorage")
local Replicated = game:GetService("ReplicatedStorage")

local StoragePillars = Replicated:WaitForChild("Pillars")
local GameFolder = workspace:WaitForChild("Game")
local Scriptable = GameFolder:WaitForChild("Scriptable")
local WorkspacePillars = Scriptable:WaitForChild("Pillars")
local AmountOfPlayers = nil

Players.PlayerAdded:Connect(function(Player)
	AmountOfPlayers = #Players:GetPlayers()

	Player.Chatted:Connect(function(Message)
		print(AmountOfPlayers)

		if string.find(string.lower(Message), "!ref") then
			for _, Pillar in ipairs(WorkspacePillars:GetChildren()) do
				Pillar:Destroy()
			end

			for _, Pillar in ipairs(StoragePillars:GetChildren()) do
				local PillarClone = Pillar:Clone()
				PillarClone.Name = "Pillars"
				PillarClone.Parent = WorkspacePillars
			end

		elseif string.find(string.lower(Message), "!start") then
			local Pillars
			repeat task.wait()
				Pillars = WorkspacePillars:GetChildren()
				Pillars[math.random(#Pillars)]:Destroy()
			until #Pillars == #Players:GetPlayers()
		
		elseif string.find(string.lower(Message), "!spawn") then
			local Spawns = WorkspacePillars:GetChildren()
			for Index, PlayerObject in ipairs(Players:GetPlayers()) do
				local CharacterModel = Player.Character
				if Spawns[Index] then
					CharacterModel:PivotTo(Spawns[Index].CFrame * CFrame.new(0, 5, 0))
				end
			end
		end
	end)
end)

Are the pillars models or parts? This script requires them to be parts.

1 Like

They are models, sadly. I do have parts for the player to spawn at.

How can I make it so it teleports all of the players to their own spot?

You can use a bunch of spawn points.

That’s not how the script is supposed to work. They only spawn on a part, not a spawnpoint.

So you want something like a teleporter?

Like I don’t want a command to do it. Basically, if you remember the !start command, once that command is ran, it will teleport their player to their own pillar.

What is the part belonging to the pillar that the player should spawn on? Give it a unique name.

sThe part name Currently is ToTele

Is it inside the pillar model?

local Players = game:GetService("Players")
local Server = game:GetService("ServerStorage")
local Replicated = game:GetService("ReplicatedStorage")

local StoragePillars = Replicated:WaitForChild("Pillars")
local GameFolder = workspace:WaitForChild("Game")
local Scriptable = GameFolder:WaitForChild("Scriptable")
local WorkspacePillars = Scriptable:WaitForChild("Pillars")
local AmountOfPlayers = nil

Players.PlayerAdded:Connect(function(Player)
	AmountOfPlayers = #Players:GetPlayers()

	Player.Chatted:Connect(function(Message)
		print(AmountOfPlayers)

		if string.find(string.lower(Message), "!ref") then
			for _, Pillar in ipairs(WorkspacePillars:GetChildren()) do
				Pillar:Destroy()
			end

			for _, Pillar in ipairs(StoragePillars:GetChildren()) do
				local PillarClone = Pillar:Clone()
				PillarClone.Name = "Pillars"
				PillarClone.Parent = WorkspacePillars
			end

		elseif string.find(string.lower(Message), "!start") then
			local Pillars
			repeat task.wait()
				Pillars = WorkspacePillars:GetChildren()
				Pillars[math.random(#Pillars)]:Destroy()
			until #WorkspacePillars:GetChildren() == #Players:GetPlayers()

			for Index, PlayerObject in ipairs(Players:GetPlayers()) do
				local CharacterModel = Player.Character
				CharacterModel:PivotTo(Pillars[Index].ToTele.CFrame * CFrame.new(0, 4, 0))
			end
		end
	end)
end)

If it is.