How to use CharacterRemoving

how do i detect like characterremoving except when the player leaves, like js detect when it dies?
i tried this and other methods but doesnt work and i cant find a script for this

local isplayerremoving = false
function ifplayerleave()
    Players.PlayerRemoving:Connect(function(player)
        isplayerremoving = true
        print("just leaving")
    end)
end

if isplayerremoving == false then
    Players.CharacterRemoving:Connect(function(player)
        print("player is dying...")
    end)
    else
    ifplayerleave()
end

If this makes no sense and I could do another method, is it still possible doing this by CharacterRemoving though?

and what is CharacterRemoving useful for

-- script (server)
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player) -- player logged in
	player.CharacterAdded:Connect(function(character) -- character (re)spawned
		local Humanoid = character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function() -- player died

		end)
	end)
end)

Players.PlayerRemoving:Connect(function(player) -- player leaving

end)

--local script (client)
local Player = game:GetService("Players").LocalPlayer -- player logged in
local Character = Player.Character or Player.CharacterAdded:Wait() -- character (re)spawned
Character:WaitForChild("Humanoid").Died:Connect(function() -- player died
    
end)

game.Players.PlayerRemoving:Connect(function(player) -- player leaving
	
end)

The best way to detect when the player died is to use the Humanoid.Died event. CharacterRemoving is good for actions that need to be performed when the character is removed when the character is reloaded or otherwise removed, even if they do not die.

local Players = game:GetService("Players")

local function CharacterAdded(Player)
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid")
	
	Humanoid.Died:Connect(function()
		-- Events
	end)
end

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		CharacterAdded(Player)
	end)
	
	if Player.Character then
		CharacterAdded(Player)
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.