How can I get when player died, left the game, or Reset?
I know left the game is PlayerRemoving()
How can I get when player died, left the game, or Reset?
I know left the game is PlayerRemoving()
Resetting counts as dying, so apart from the PlayerRemoving
event, you should use the Humanoid.Died
event to know whenever a player resets or dies.
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.Died:Connect(function()
print(Player, "died!")
end)
More info: Humanoid.Died
You can utilize RBXScriptSignals that roblox provides for you.
Player Removing Signal:
game.Players.PlayerRemoving
Player Death Signal:
player.Character.Humanoid.Died
The two above RBXScriptSignals can be connected to developer defined functions using :Connect(f:function). For example, game.Players.PlayerRemoving:Connect(function() end)
.
Core Script Override for Resetting:
Detecting when a player resets is a little different. It is already handled by roblox core scripts, so we need to actually override that default befavior in a local script using :SetCore()
local resetBindable = Instance.new("BindableEvent") --make a bindable event to fire when the player clicks reset
resetBindable.Event:Connect(function() --define what happens if/when the player does click the reset button
print("Reset Button Clicked") --custom behavior :)
game.Players.LocalPlayer.Character.Humanoid.Health = 0 --We still want to reset the character after our custom behavior
end)
game:GetService("StarterGui"):SetCore("ResetButtonCallback",resetBindable) --tell roblox core scripts to fire this event when the player resets