Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to detect when player leaves server?

Hello! I need a script where when a player leaves a part gets destroyed. This is when a player dies.


game:GetService('Players').PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("Humanoid").Died:Connect(function()
            local w = workspace.Clones:GetChildren()
            for i = 1, #w do
                if w[i].Name == player.Name .. " Clone" or  w[i].Name == "Player.character.Name" or w[i].Name == "Player.Name" then
                    w[i]:Destroy()
                end
            end
        end)
    end)
end)

How do I make it so when the player leaves, the part gets destroyed ? I have tried this but it doesn’t work, how do I do it?


game:GetService('Players').PlayerAdded:Connect(function(player)
    player.CharacterRemoving:Connect(function(character)
        character:WaitForChild("Humanoid").Died:Connect(function()
            local w = workspace.Clones:GetChildren()
            for i = 1, #w do
                if w[i].Name == player.Name .. " Clone" or  w[i].Name == "Player.character.Name" or w[i].Name == "Player.Name" then
                    w[i]:Destroy()
                end
            end
        end)
    end)
end)

4 answers

game.Players.PlayerRemoving:Connect(function(player)
	-- code to destory a part 
end)
game.Players.PlayerRemoving:Connect(function(player)
	-- code to destory a part 
end)

Use the PlayerRemoving function to achieve this.

local Part = game.Workspace.Part -- change this to where the part is

game.Players.PlayerRemoving:Connect(function()
    Part:Destroy()
end)

You can just do game.Players.PlayerRemoving

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

This discussion is preserved as it was. New answers, comments, and votes are closed.