PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

RespawnLocation

Player

SpawnLocation

If set, the player will respawn at the given SpawnLocation. This property can only be set through Lua and must contain a reference to a valid SpawnLocation, which must meet the following criteria:

  • Descendant of Workspace
  • SpawnLocation/TeamColor|SpawnLocation.TeamColor is set to the Player/TeamColor|Player.TeamColor or SpawnLocation/Neutral|SpawnLocation.Neutral is set to true

If RespawnLocation is not set to a valid SpawnLocation then the default spawning logic will apply. For more information on this see the page for SpawnLocation.

Alternatives to RespawnLocation

  • A Player will spawn from SpawnLocation|SpawnLocations belonging to their team. In some cases it may be simpler to change the Player|Player's Player/Team instead. This method also allows the Player to respawn from multiple SpawnLocation|SpawnLocations.
  • Developers may wish to implement their own custom spawn logic, using Model/SetPrimaryPartCFrame to move the Player/Character manually.

Code Samples


Change Spawn on Touch

This code sample will set the player to always respawn from the last SpawnLocation they touched. New players will respawn from the SpawnLocation named ‘FirstSpawn’ until they touch a different SpawnLocation.

This is an alternative to using the AllowTeamChangeOnTouch property to switch SpawnLocations and does not require Teams.

local Players = game:GetService("Players")

local function addSpawn(spawnLocation)
	-- listen for the spawn being touched
	spawnLocation.Touched:Connect(function(hit)
		local character = hit:FindFirstAncestorOfClass("Model")
		if character then
			local player = Players:GetPlayerFromCharacter(character)
			if player and player.RespawnLocation ~= spawnLocation then
				local humanoid = character:FindFirstChildOfClass("Humanoid")
				-- make sure the character isn't dead
				if humanoid and humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
					print("spawn set")
					player.RespawnLocation = spawnLocation
				end
			end
		end
	end)
end

local firstSpawn

-- look through the workspace for spawns
for _, descendant in pairs(workspace:GetDescendants()) do
	if descendant:IsA("SpawnLocation") then
		if descendant.Name == "FirstSpawn" then
			firstSpawn = descendant
		end
		addSpawn(descendant)
	end
end

local function playerAdded(player)
	player.RespawnLocation = firstSpawn
end

-- listen for new players
Players.PlayerAdded:Connect(playerAdded)

-- go through existing players
for _, player in pairs(Players:GetPlayers()) do 
	playerAdded(player)
end