Opinion on my player death notification code

Hello devs, this is the code I made for a system that notifies all players through a text label about a player’s death in the experience. I’d like to hear your opinions on whether it’s correct, if anything needs to be changed, or if it could be organized better. I look forward to your feedback.

This is how looks like:

https://youtu.be/cescfwJ22wM?si=cLwrIPfbm8BTmSMS

This is how I have each object in the explorer:

Captura de pantalla 2024-11-09 162622

Scripts codes:

PlayerDied:

local OnPLayerDeath = game:GetService('ReplicatedStorage').OnPlayerDeath

local PlayerCharacter = script.Parent
local Humanoid = PlayerCharacter:FindFirstChild('Humanoid')

local function SendPlayerName()
	OnPLayerDeath:FireServer(PlayerCharacter.Name)
end

Humanoid.Died:Connect(SendPlayerName)

SendPlayerName:

local OnPLayerDeath = game:GetService('ReplicatedStorage').OnPlayerDeath

OnPLayerDeath.OnServerEvent:Connect(function(PlayerName)
	OnPLayerDeath:FireAllClients(PlayerName)
end)

ShowPlayerDied:

local TweenService = game:GetService('TweenService')

local OnPLayerDeath = game:GetService('ReplicatedStorage').OnPlayerDeath

local PlayerDiedScreen = script.Parent
local TextLabel = PlayerDiedScreen.TextLabel
local OGText = 'has died.'

local TWEEN_CONFIG = {
	TWEEN_INFO = TweenInfo.new(.5),
	
	TRANSITION = {
		MAX = 1,
		MIN = 0
	}
}

local TEXT_TWEEN = {
	APPEAR = TweenService:Create(TextLabel, TWEEN_CONFIG.TWEEN_INFO, {
		TextTransparency = TWEEN_CONFIG.TRANSITION.MIN
	}),

	DISAPPEAR = TweenService:Create(TextLabel, TWEEN_CONFIG.TWEEN_INFO, {
		TextTransparency = TWEEN_CONFIG.TRANSITION.MAX
	}),
}

OnPLayerDeath.OnClientEvent:Connect(function(PlayerName) 
	TextLabel.Text = tostring(PlayerName)..' '..OGText
	
	TEXT_TWEEN.APPEAR:Play()
	
	TEXT_TWEEN.APPEAR.Completed:Connect(function()
		task.wait(3)
		TEXT_TWEEN.DISAPPEAR:Play()
	end)
end)

could you add a death cause?

something like “[PLAYER] HAS DIED: [CAUSE OF DEATH]” would be nice

1 Like

That will be the next step (though I’m not sure how to do it yet),
for now, I’m basing it on the system from THE RAKE CLASSIC EDITION

The only problem with this code is that an exploiter could spam the “OnPlayerDeath” event and flood/crash others screen, you could try handling the Humanoid.Died part on the server, though even that could be exploited by someone setting their health to 0 over and over again.

The way I would go about this (I’m guessing you’re going to have some sort of “Round” system), is making a table and storing all of the players currently in the game in it, when a player dies, take them out of it, and when a player respawns the next round, put them back in.

Then, in the OnPlayerDeath.OnServerEvent function, I would check if the player is in that table, then fire all of the clients. (This might not be the best way, but its simple and works!)