Respawn & Death System

RESPAWN & DEATH SYSTEM

I got bored and decided to create a respawn and death module. It just lets you configure settings listed below.

Settings_Config:

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- waterboy, 2024
-- Respawn & Death Config
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
local Settings_Config = {}

Settings_Config.Settings = {
	
	-- toggleables
	RespawnAtDeathLocation = true,     		  -- respawn player where they died
	RespawnAtRandomLocation = false,  		  -- respawn player at a "random" location
	RespawnWithForcefield = false,     		  -- respawn with forcefield
	R6RagdollOnDeath = true,           		  -- enable ragdoll on death
	ToggleRagdoll = true,             		  -- toggle ragdoll
	QuickRespawn = true,              		  -- toggle quick respawn
	BloodOnDeath = true,			   		  -- blood droplets on death
	DeathSoundEffect = true,				  -- sfx on death
	
	-- number values
	RespawnYPositionOffset = 2,        		  -- offset from the ground on respawn
	RespawnDelay = 3,                 		  -- delay before respawn after death
	RaycastDistance = -5,              		  -- distance for the raycast to detect ground
	ForcefieldDuration = 3,            		  -- duration of the forcefield in seconds
	RagdollOnDeathHeadVelocityX = 0,          -- custom x velocity for head
	RagdollOnDeathHeadVelocityY = 0,          -- custom y velocity for head
	RagdollOnDeathHeadVelocityZ = 0,          -- custom z velocity for head
	
	-- keybinds
	ToggleRagdollKey = "R",            	      -- ToggleRagdoll keybind
	QuickRespawnKey = "F",                    -- QuickRespawn keybind
	ToggleRagdollController = "ButtonB",      -- ToggleRagdoll keybind controller
	QuickRespawnController = "ButtonX"        -- QuickRespawn keybind controller
}

return Settings_Config

The RespawnAtDeathLocation is a little inconsistent, I might re-work it later on and ToggleRagdoll can be a bit glitchy at times, I have a “fix” but even then its still glitchy, I might revert it to its original state because sometimes the camera will glitch and I would rather have a wonky camera then a glitched one.

I don’t know what else do add to this so any suggestion is helpful. Let me know if this is even useful because I made it with the hopes of it actually being somewhat helpful.

Demonstration Video:

Get Module Here

Module Link Here - Creator Store

Updates
0.3

Update 0.3

  • Added mobile and controller support
  • Added custom death sound
  • Added the ability to toggle custom death sound in Config module
0.2

Update 0.2

0.1

Update 0.1

  • VERY Useful
  • Somewhat useful
  • Meh
  • bad

0 voters

11 Likes

I made something similar to this all the way back in 2016 for my Serious Sam Roblox game.

Curious how you remembered the spot they jumped from before they died?

Really neat stuff! :smiley:

Its like a checkpoint/flag system, a ray is shot down from the rootpart 5 studs and stores the players position in a table. once the player dies they then spawn at that position and the positions that were saved are then cleared.

I have it shoot down 5 studs so that if the player is on an object the ray wouldn’t be long enough to touch the part/ground to save their position, therefore spawning at the last saved position.

There might be better ways of doing this but this is the only thing I could think of to do this.

Code Snippet:

-- update player position and store the ground location
function PlayerModule.updatePlayerPosition(player)
	local character = player.Character
	if not character then return end

	local humanoid = character:FindFirstChildOfClass("Humanoid")
	local primaryPart = character.PrimaryPart
	if not humanoid or not primaryPart or humanoid.Health <= 0 then return end

	-- raycast parameters to ignore the character
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = {character}

	-- raycast downward from the character's position
	local rayStartPosition = primaryPart.Position
	local raycastResult = workspace:Raycast(rayStartPosition, Vector3.new(0, Config.Settings.RaycastDistance, 0), raycastParams)

	if raycastResult then
		print("Ray hit the ground at:", raycastResult.Position)
		local yOffset = primaryPart.Size.Y / 2
		deathPos[player] = CFrame.new(raycastResult.Position + Vector3.new(0, yOffset + Config.Settings.RespawnYPositionOffset, 0))
	end
end

-- character initialization and respawn logic
function PlayerModule.onCharacterAdded(player, character)
	local player = Players:GetPlayerFromCharacter(character)
	if not player then return end

	RunService.Heartbeat:Wait() -- ensure character is fully loaded

	-- respawn at "random" location if RespawnAtRandomLocation is true
	if Config.Settings.RespawnAtRandomLocation then
		local randomPosition = getRandomSpawnPosition()
		character:SetPrimaryPartCFrame(CFrame.new(randomPosition))
	end

	-- respawn at death location if RespawnAtDeathLocation is true
	if Config.Settings.RespawnAtDeathLocation and deathPos[player] then
		character:SetPrimaryPartCFrame(deathPos[player])
	end

	-- continuously update player position
	local timer = 0
	local connection

	connection = RunService.Heartbeat:Connect(function(deltaTime)
		timer = timer + deltaTime

		if timer >= 0.01 then
			PlayerModule.updatePlayerPosition(player)
			timer = 0
		end
	end)

	connections[player] = connection

	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		
		-- set the RespawnTime in the Players properties as the RespawnDelay
		Players.RespawnTime = Config.Settings.RespawnDelay

		-- R6 ragdoll on death if R6RagdollOnDeath is true
		humanoid.Died:Connect(function()
			if Config.Settings.R6RagdollOnDeath then
				ragdollPlayer(character)
			end

			-- set the RespawnDelay
			task.wait(Config.Settings.RespawnDelay)
			player:LoadCharacter()

			-- clear the previous death position only after respawn at death location
			local newCharacter = player.Character
			if newCharacter and Config.Settings.RespawnAtDeathLocation and deathPos[player] then
				RunService.Heartbeat:Wait() -- ensure character is fully loaded after respawn
				newCharacter:SetPrimaryPartCFrame(deathPos[player])

				-- once the player is placed at the death position, clear it
				deathPos[player] = nil
			end

			-- forcefield if RespawnWithForcefield is true
			if Config.Settings.RespawnWithForcefield then
				local forcefield = Instance.new("ForceField")
				forcefield.Parent = newCharacter

				-- set the ForcefieldDuration
				task.delay(Config.Settings.ForcefieldDuration, function()
					if forcefield and forcefield.Parent then
						forcefield:Destroy()
					end
				end)
			end

			if connections[player] then
				connections[player]:Disconnect()
				connections[player] = nil
			end
		end)
	end
end
1 Like

could you possibly just check if player is airborn, using the enum value that is passed for what they are standing on

Update 0.2

Will add mobile and controller support next update, along with some other things, hopefully.

Update 0.3

  • Added mobile and controller support
  • Added custom death sound
  • Added the ability to toggle custom death sound in Config module

Give me ideas on what to add next!

make it so you explode on death trust me :coefficients:

1 Like

#1 what is that emoji and how do I use it.

#2 the errors are not helping

#3 other then that I like the model, you could probably add a little sparkle or a option for one where your spawn is.

:coefficients: FOUND IT

1 Like

the coefficients emoji is by far the best emoji in the entire devforum

3 Likes

Literally fax bro i’ve been using it ever since!! :coefficients:

1 Like