Instance duplicating everytime player respawns after destroyed

Hi i made a script where it runs multiple stuffs when playerAdded, characterAppearanceLoaded, remoteEvent onServerEvent.

The problem is, after the player dies, the folder is destroyed as expected. But when the remoteEvent gets fired again(after the player’s death), another folder should be created but instead theres TWO created folders, and so goes on by adding +1 folder each time the player dies and fires the event again which isnt what i want.(1,2,3,4,5 folders, etc)
local script: (textbutton)

local rm = game.ReplicatedStorage.wormRemotes.addWorm
script.Parent.MouseButton1Click:Connect(function()
	rm:FireServer()
end)

server script:

local PhysicsService = game:GetService("PhysicsService")
local obstacles = "obstacles"
local pass = "pass"
PhysicsService:CreateCollisionGroup(obstacles)
PhysicsService:CreateCollisionGroup(pass)
PhysicsService:CollisionGroupSetCollidable(pass, obstacles, false)

local rm = game.ReplicatedStorage.wormRemotes.addWorm

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		rm.OnServerEvent:Connect(function()
			local folder = Instance.new("Folder") --line where creates folder
			folder.Parent = workspace
			folder.Name = player.Name.."'s Folder"
-- from this line, its just some stuffs i made
			for i,v in pairs(character:GetDescendants()) do
				if v:IsA("BasePart") then
					PhysicsService:SetPartCollisionGroup(v, pass)
					v.Transparency = 1
				else
					if v:IsA("Decal") then
						v.Transparency = 1
					end
				end
			end

			local start = Instance.new("Part", workspace)
			start.Name = player.Name.."'s followPart"
			start.Transparency = 1
			start.CanCollide = false
			start.Anchored = false
			start.Massless = true
			start.CFrame = character.HumanoidRootPart.CFrame
			PhysicsService:SetPartCollisionGroup(start, obstacles)
			local weld = Instance.new("Weld", start)
			weld.Part0 = start
			weld.Part1 = character.HumanoidRootPart
			weld.C0 = CFrame.new(0,2,0)
			local finish = start
			character.Head.Position = start.Position
-- end of stuffs i made
			character.Humanoid.Died:Connect(function(player) --line where destroys folder
				folder:Destroy()
				start:Destroy()
			end)
		end)
	end)
end)

I believe the whole CharacterAppearanceLoaded function is being called twice, so it creates two folders. The first time you called the function, the function’s nested remote function is still active, so when you call it AGAIN, its calling the remote function twice. Hope that makes sense cause I am bad at explaining. You can just add a simple debounce to fix your problem.

1 Like

The debounce would have to be a local debounce tho.

1 Like

i quite understand what you mean, but how do i make a debounce local??

did u mean in a block scope? like this?

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		local db = false --debounce
		if db == false then
			db = true
			rm.OnServerEvent:Connect(function()
				local folder = Instance.new("Folder")
				folder.Parent = workspace
				folder.Name = player.Name.."'s Folder"

				character.Humanoid.Died:Connect(function()
					folder:Destroy()
				end)
			end)
			db = false
		end
	end)
end)

Yeah like that. However, you shouldn’t set the debounce back to false since the function isn’t going to run again. A new function will be called, creating a new local debounce for the new local functions.
You’ll want some sort of condition on every nested function so they stop when the function ends:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		local db = false --debounce
			rm.OnServerEvent:Connect(function()
                if db == false then
			       db = true
				   local folder = Instance.new("Folder")
				   folder.Parent = workspace
				   folder.Name = player.Name.."'s Folder"

			  	   character.Humanoid.Died:Connect(function()
					   folder:Destroy()
				   end)
               end
		end)
	end)
end)