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

Respawn at Despawn Location

Respawn at Despawn Location

Oct 01 2018, 1:26 AM PST

This code sample will cause players to respawn at the same place they died. It does this by keeping track of where the player despawned using Player/CharacterRemoving. Note that the player’s location is saved on-despawn, not on-death. This can be problematic if the player falls off a ledge and dies due to Workspace/FallenPartsDestroyHeight - their respawn position won’t be saved in this case.

It’s also important to note the need to “forget” the location of players who leave the game. We use Instance/ChildRemoved on Players instead of Players/PlayerRemoving. This is because PlayerRemoving fires before CharacterRemoving - and we need to make sure we don’t forget the player’s respawn location then immediately remember a new one (this is a memory leak; potentially many players could visit, respawn and leave). So, we use ChildRemoved on Players so the event fires after the character is removed.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

-- This table maps "Player" objects to Vector3
local respawnLocations = {}

local function onCharacterAdded(character)
	local player = Players:GetPlayerFromCharacter(character)
	-- Check if we saved a respawn location for this player
	if respawnLocations[player] then
		-- Teleport the player there when their HumanoidRootPart is available
		local hrp = character:WaitForChild("HumanoidRootPart")
		-- Wait a brief moment before teleporting, as Roblox will teleport the
		-- player to their designated SpawnLocation (which we will override)
		RunService.Stepped:wait()
		hrp.CFrame = CFrame.new(respawnLocations[player] + Vector3.new(0, 3.5, 0))
	end
end

local function onCharacterRemoving(character)
	-- Get the player and their HumanoidRootPart and save their death location
	local player = Players:GetPlayerFromCharacter(character)
	local hrp = character:FindFirstChild("HumanoidRootPart")
	if hrp then
		respawnLocations[player] = hrp.Position
	end
end

local function onPlayerAdded(player)
	-- Listen for spawns/despawns
	player.CharacterAdded:Connect(onCharacterAdded)
	player.CharacterRemoving:Connect(onCharacterRemoving)
end

local function onPlayerRemoved(player)
	-- Forget the respawn location of any player who is leaving; this prevents
	-- a memory leak if potentially many players visit
	respawnLocations[player] = nil
end

-- Note that we're NOT using PlayerRemoving here, since CharacterRemoving fires
-- AFTER PlayerRemoving, we don't want to forget the respawn location then instantly
-- save another right after 
Players.PlayerAdded:Connect(onPlayerAdded)
Players.ChildRemoved:Connect(onPlayerRemoved)