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:
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)