Create a folder when a player joins and destroy it when they leave

Basically i am making a script to create a folder with a player’s name when they join the game and delete it when they leave.

I’m just having some trouble at the deleting part, this is my script so far:

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder", workspace.Horses)
	folder.Name = plr.Name
end)

game.Players.PlayerRemoving:Connect(function(plr)
	game.Workspace.Horses:FindFirstChild(plr.Name):Destroy()
end)

Note: “Horses” is a folder in workspace.

The creating part will work just fine but i’m just not sure if the removing part will work (as i can’t check because i would’ve left the game).

So if someone could tell me if this would work or another way to do it i would very much appreciate it, thanks.

2 Likes

You can test this by starting up a local server with 2 players. Leave with one of the 2 clients and check workspace.Horses. If the folder is deleted then your script works. I don’t see anything wrong with your script here.

How would i start a local server with 2 players inside studio?

Go on the test tab and there should be a button with a screen on it, if I’m not mistaken.

It explains the steps very well on this website: https://developer.roblox.com/en-us/articles/game-testing
Scroll down to multi client simulation

You should not be using the second argument of Instance.new

2 Likes

image

Go to the Test menu and check it with two or more players as you want and see what will happen in the workspace

yeah this will work

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder", workspace.Horses)
	folder.Name = plr.Name
        game.Players.PlayerRemoving:Connect(function(plr2)
                if plr == plr2 then
                      folder:Destroy()
                end
        end)
end)
1 Like