GameManagerModule.on("playerdeath", function(playerWhoDied) --player instance, not name
print(playerWhoDied.Name..." died!")
end)
Have a client sided script that activates a remote event whenever they die, and a death index / value. That index would be put into a table to check what the death reason is. The index could be a string with the death reason, but that may over complicate things because you would have to input the death reason from a different script, which could break the system if you input a wrong reason.
Example of a script like that:
local deathMessage = require(script.Parent.DeathMsg).msg --// Module scripts are more reliable that value instances, since if the script just wont require if it doesn't exist, while if it was an instance, it would either throw an error or a warning
local hmoid = script.Parent.Character:WaitForChild("Humanoid") --// This is in StarterPlayerScripts since script.Parent is the player, not the character
local deathEvent = game:GetService("ReplicatedStorage").DeathEvent --// Or the location of the remote event
local died = false
hmoid.Died:Connect(function()
if not died then
died = true
deathEvent:FireServer()
task.wait(game:GetService("Players").RespawnTime) --// I can't remember the exact name of the respawn time property, so replace it with the real name if it doesn't work
died = false --// Debounce just incase something goes wrong
end
end)
Note: Code was written without testing.
Note: I wrote the code since I for whatever reason find it to be incredibly entertaining.
Note: I would recommend testing this code in a baseplate before marking anything as the solution, since I came up with this on the fly lol.
Humanoid.Died is what youre looking for
Module:
local module = {}
local EventObjects = script:FindFirstChildWhichIsA("Folder") or Instance.new("Folder", script)
EventObjects.Name = "EventObjects"
function module.getEvent(name)
local event = EventObjects:FindFirstChild(name)
if not event then
event = Instance.new("BindableEvent")
event.Name = name
event.Parent = EventObjects
end
return event
end
module.events = {
["playerdeath"] = module.getEvent("playerdeath")
}
function module.on(event, func)
local BindableEvent = module.events[event]
if not BindableEvent then
warn("event was not found")
return nil
end
BindableEvent.Event:Connect(func)
end
function module.fire(event, ...)
local BindableEvent = module.events[event]
if not BindableEvent then
warn("event was not found")
return nil
end
BindableEvent:Fire(...)
end
return module
Script 1 firing the event:
local Players = game:GetService("Players")
local GameManagerModule = require(script.Parent)
function PlayerAdded(player)
local function CharacterAdded(char)
local hum = char:WaitForChild("Humanoid", 5)
if not hum then return end
hum.Died:Connect(function()
GameManagerModule.fire("playerdeath", player)
end)
end
CharacterAdded(player.Character or player.CharacterAdded:Wait())
player.CharacterAdded:Connect(CharacterAdded)
end
for i, player in pairs(Players:GetPlayers()) do
PlayerAdded(player)
end
Players.PlayerAdded:Connect(PlayerAdded)
Script 2 listening the event:
local GameManagerModule = require(script.Parent)
GameManagerModule.on("playerdeath", function(player)
print(player.Name)
end)