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

How do I make this cannonball be deleted after existing for a certain time. Help?

I have a block that you can click on that shoots a part. and I want the cannonball to disappear for existing for longer than about 5 seconds Help?

Code here V



local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:Connect(function()
	
	local copy = game.Workspace.Part:Clone()
	
	copy.Parent = workspace
	copy.Position = game.Workspace.Cannon.Position
	copy.Anchored = true
	while true do
		
		copy.Position = copy.Position + Vector3.new (3,0,0)
		wait(0.05)
		
		
		
		
	end
	
	
	
end)

1 comment

0

Everything you need now is Debris service https://developer.roblox.com/en-us/api-reference/class/Debris

1 answer

✓ Accepted answer

To destroy it, there are many methods. You can use a for loop like so, and use either :Destroy() after the part or get Debris Service and use that.

--Put Debris Service here if you use Debree Method this is the code: local DebrisService = game:GetService("Debris")

local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:Connect(function()

    local copy = game.Workspace.Part:Clone()

    copy.Parent = workspace
    copy.Position = game.Workspace.Cannon.Position
    copy.Anchored = true
   --replace while true do with for i = 1,5, 0.05 do 
while true do

        copy.Position = copy.Position + Vector3.new (3,0,0)
        wait(0.05)
    end
end)
--Use DebreeService:AddItem(Part, 0) or Part:Destroy()

1 comment

0

Thank you!

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