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.
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