Remove Folder When Player Leave the game

Hi!
How to remove folder from workspace when player leave from the game?

This is my script:

local prompt = script.Parent
local appPlayer = nil
local plot = script.Parent.Parent.Parent.Parent

prompt.Triggered:Connect(function(player1)
	appPlayer = player1
	player1.PlayerData.IsPlayerHavePlot.Value = true
	script.Parent.Parent.Parent.Parent.Claimed.Value = true
	script.Parent.Enabled = false
	local office = game.ReplicatedStorage.Models.Offices.OfficeLvl1
	local clone = office:Clone()
	clone.Parent = plot.Offices
	local model = clone
	model:MoveTo(plot.Position)
end)

game.Players.PlayerRemoving:connect(function(player) -- This part
	if player.Name == appPlayer then
		prompt.Enabled = true
		script.Parent.Parent.Parent.Parent.Claimed.Value = false
		plot.Offices:Destroy()
		Instance.new("Folder", plot).Name = "Offices"
	end
	end)
game.Players.PlayerRemoving:connect(function(player) -- This part
Instance.new("Folder", plot).Name = "Offices"


this line, why in the world you will create a Folder, if the player is leaving?
you dind’t wanted to Delete it? so why you are creating it…

I want to delete this folder to remove all models in this folder

Ok, I solved this now :sunglasses:

Thanks for all replies

Don’t use Instance.new("Folder", plot), it’s a lot slower and can do some harm to your game, I would link a topic explaining this but I can’t find it, just use:

local folder = Instance.new("Folder")
folder.Name = "Offices"
folder.Parent = plot
1 Like
1 Like
local prompt = script.Parent
local appPlayer = nil
local plot = script.Parent.Parent.Parent.Parent
local players = game:GetService("Players")
local storage = game:GetService("ReplicatedStorage")

prompt.Triggered:Connect(function(player)
	appPlayer = player
	player.PlayerData.IsPlayerHavePlot.Value = true
	script.Parent.Parent.Parent.Parent.Claimed.Value = true
	script.Parent.Enabled = false
	local office = storage.Models.Offices.OfficeLvl1
	local clone = office:Clone()
	clone.Parent = plot.Offices
	local model = clone
	model:MoveTo(plot.Position)
end)

players.PlayerRemoving:connect(function(player) -- This part
	if player.Name == appPlayer.Name then
		prompt.Enabled = true
		script.Parent.Parent.Parent.Parent.Claimed.Value = false
		plot.Offices:Destroy()
	end
end)

You were attempting to compare a player instance with a player name (which is a string value) which would always end up as evaluating as false.