Needing Help > Death Message System

How to make a Death Message System showcased in the video below.

2 Likes

1 Like
local Players = game:GetService("Players")
local RespawnTime = Players.RespawnTime
local playerMessages = {}

local function OnDeath()
	local humanoid = script.Parent:FindFirstChild("Humanoid")
	if not humanoid then
		warn("Humanoid not found in " .. script.Parent.Name)
		return
	end

	local killer = humanoid:FindFirstChild("creator") and humanoid.creator.Value
	local deathMessage = script.Parent.Name .. " was eliminated."

	if killer and killer:IsA("Player") then
		deathMessage = script.Parent.Name .. " was eliminated by " .. killer.Name .. "."
	end

	for _, player in pairs(Players:GetPlayers()) do
		local playerGui = player:FindFirstChild("PlayerGui")
		if playerGui then
			local Gui = script.DiedGui:Clone()
			Gui.Parent = playerGui
			Gui.DiedText.Text = deathMessage

			if not playerMessages[player] then
				Gui.DiedText.Position = UDim2.new(0.5, 0,0, -20)
			else
				local existingMessageCount = 0
				for _, child in ipairs(playerGui:GetChildren()) do
					if child:IsA("ScreenGui") and child:FindFirstChild("MessageText") then
						existingMessageCount = existingMessageCount + 1
					end
				end

				local yOffset = existingMessageCount * 30
				Gui.DiedText.Position = UDim2.new(0.5, 0, 0.1, yOffset)
			end

			playerMessages[player] = playerMessages[player] or {}
			table.insert(playerMessages[player], Gui)

			task.delay(RespawnTime, function()
				if Gui.Parent then
					Gui:Destroy()
					table.remove(playerMessages[player], 1)
					if #playerMessages[player] == 0 then
						playerMessages[player] = nil
					end
				end
			end)
		else
			warn("PlayerGui not found for " .. player.Name)
		end
	end
end

script.Parent.Humanoid.Died:Connect(OnDeath)

Players.PlayerRemoving:Connect(function(player)
	if playerMessages[player] then
		for _, msg in ipairs(playerMessages[player]) do
			msg:Destroy()
		end
		playerMessages[player] = nil
	end
end)
2 Likes

Here are some steps you could follow, if you need more explained im happy to asisst further;

  1. Make a ScreenGui and in the Gui a frame. Set the frame size and position to wherever you would like the messages to be. presonally i would use scale but you can choose either scale or offset,
  2. Add an UiListLayout inside the frame. this will make ui objects in the frame automatically be put in a list format like shown in the video
  3. Make a textlabel and customise it then store it somewhere of your choice, aslong as it is accesible by the client. This textlabel should look like how you want the messages to look like
  4. Make a localscript which copies the stored textlabel into our frame and deletes it after a while when someone dies. The local script should also set the text of the textlabel to the correct text. Once again, im willing to help you more on this if you want.
1 Like

Is this a relevant layout?
Screenshot 2024-11-13 022752

1 Like

Yes, That could work. Assuming DiedText is the text which pops up when the player dies

Yes that is correct, how will the Local Script look like?

Let me write something real quick, give me a few minutes

Try using this Template >
Death Message Template.rbxm (6.7 KB)

1 Like

Here is some code:

local Debris = game:GetService("Debris")

local KillMessage = " Was killed by " --Include space infront and on the end
local SecondsUntilDissapear = 4

local SpawnMessage = function(Victim, Killer)
	local Clone = script.DiedText:Clone()
	Clone.Text = Victim .. KillMessage .. Killer
	Clone.Parent = script.Parent
	Clone.Visible = true
	Debris:AddItem(Clone, SecondsUntilDissapear)
end

--I dont know what system your game uses to detect if a Victim has died but here are some example you can use

--If you can get the signal trough an event:
game.Something.Something.Event:Connect(function(Victim, Killer)
	if Killer then
		SpawnMessage(Victim, Killer)
	end
end)

--If killer is a string attribute of Victim humanoid:
game.Players.PlayerAdded:Connect(function(Victim)
	Victim.CharacterAdded:Connect(function(Character)
		Character:WaitForChild("Humanoid").Died:Once(function()
			local Killer = Character.Humanoid:GetAttribute("Killer")
			if Killer then
				SpawnMessage(Victim.Name, Killer)
			end
		end)
	end)
end)

also use this layout for this to work:
image
Names on things dont matter except for “DiedText”

1 Like

The Script is based on the Server though… Everybody views the message.

That is true, but the script runs for everyone so it will still work. in this case a localscript is the better option i believe

Can’t I use a Server Script and put inside of StarterCharacterScripts

ServerScripts wont run there. Only LocalScripts will My bad, i tought you said starterplayerscripts

You could do that but that would come with unnecessary performance issues. all gui handling should generally be done on the client

@svenska_kartongbit

How is this concept?

local Players = game:GetService("Players")
local RespawnTime = Players.RespawnTime
local playerMessages = {}

local function OnDeath()
	local humanoid = script.Parent:FindFirstChild("Humanoid")

	if not humanoid then
		warn("Humanoid not found in " .. script.Parent.Name)
		return
	end

	local killer = humanoid:FindFirstChild("creator") and humanoid.creator.Value
	local deathMessage = script.Parent.Name .. " was eliminated."

	if killer and killer:IsA("Player") then
		deathMessage = script.Parent.Name .. " was eliminated by " .. killer.Name .. "."
	end

	for _, player in pairs(Players:GetPlayers()) do
		local playerGui = player:FindFirstChild("PlayerGui")
		if playerGui then
			local Gui = script.DiedGui:Clone()
			local DiedFrame = Gui:WaitForChild("DiedFrame")
			local DiedText = DiedFrame:WaitForChild("DiedText")

			DiedText.Text = deathMessage
			Gui.Parent = playerGui
			DiedFrame.Visible = true

			playerMessages[player] = playerMessages[player] or {}
			table.insert(playerMessages[player], Gui)

			player.CharacterAdded:Connect(function()
				task.delay(RespawnTime, function()
					if Gui.Parent then
						Gui:Destroy()
						table.remove(playerMessages[player], 1)
						if #playerMessages[player] == 0 then
							playerMessages[player] = nil
						end
					end
				end)
			end)
		else
			warn("PlayerGui not found for " .. player.Name)
		end
	end
end

script.Parent.Humanoid.Died:Connect(OnDeath)

Players.PlayerRemoving:Connect(function(player)
	if playerMessages[player] then
		for _, msg in ipairs(playerMessages[player]) do
			msg:Destroy()
		end
		playerMessages[player] = nil
	end
end)
1 Like

If any other adjustments are needed feel free to say so, thank you.

I dont really understand how that one is supposed to work since i dont know what creator is and other stuff so i cant help you with that, sorry