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

How to delete an object when a player leaves?

I've been trying to delete a specific object in the workspace when a specific player leaves.

There's a folder full of int values, the int values hold the names of the players that are in the game, when one of the players leave, I want the int value with their name to be removed. Any idea how to do this? I've tried PlayerRemoving and a loop to find if the player was in the game or not but I couldn't get them to work. PlayerRemoving deleted the wrong value, and the loop just deleted all of them. Here is the loop that I used:

wait(1)
local Player = script.Parent.Parent.Parent



while true do
	wait()
	local Entry = workspace.Roster:FindFirstChild(Player.Name)
	if Entry ~= nil then
		local PlayEntry = game.Players:FindFirstChild(Entry)
		if PlayEntry == nil then
			Entry:Destroy()
		end
	end
end

Thanks for reading

1 answer

✓ Accepted answer

Hello, soutpansa!

you can use the PlayerRemoving event!

Working script:

game.Players.PlayerRemoving:Connect(function(Player)
	local Entry = workspace.Roster:FindFirstChild(Player.Name)
    if Entry ~= nil then
        local PlayEntry = game.Players:FindFirstChild(Entry)
        if PlayEntry == nil then
            Entry:Destroy()
        end
    end
end)

Good Luck with your games!!!

2 comments

0

It seemed like your solution worked. I'd like to do some more testing before I accept the answer though. Thanks!

0

ok (I know this will work)

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