Deleting cloned part in a for loop

Hey, I am working on this move where I clone 5 parts and after a second it will be destroyed. But I got stuck on the deleting part because I don’t want to destroy the part in the loop of course.

remote.OnServerEvent:Connect(function(player, MouseHit)
	local Character = player.Character
	local Humanoid = Character:FindFirstChild("Humanoid")
	local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
	local Clone = Assets.TestNeedle:Clone()
	
	for count = 1 , 5 do
		local Clone = Assets.TestNeedle:Clone()
		Clone.Parent = workspace.Map.FX
		Clone.Anchored = true 
		Clone.Position = HumanoidRootPart.Position + Vector3.new(math.random(-5,5), math.random(7,15) , math.random(-7,7))
		Clone.CFrame = CFrame.lookAt(Clone.Position, MouseHit.Position)
		
	end
	
	
end)

Where should I put the delete part for now and in the future ?

Thanks for helping!!

You can use debris service. It would look something like this:

local debris = game:GetService("Debris")
remote.OnServerEvent:Connect(function(player, MouseHit)
	local Character = player.Character
	local Humanoid = Character:FindFirstChild("Humanoid")
	local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
	local Clone = Assets.TestNeedle:Clone()
	
	for count = 1 , 5 do
		local Clone = Assets.TestNeedle:Clone()
		Clone.Parent = workspace.Map.FX
		Clone.Anchored = true 
		Clone.Position = HumanoidRootPart.Position + Vector3.new(math.random(-5,5), math.random(7,15) , math.random(-7,7))
		Clone.CFrame = CFrame.lookAt(Clone.Position, MouseHit.Position)
		Debris:AddItem(clone,1)
	end
	
	
end)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.