Destroying a Ball when it touches another Part isn't working for me

  1. What do you want to achieve? I want to Destroy balls when they touch an Anchored “Collector” Part after falling & bouncing off a series of Anchored Parts.

  2. What is the issue? The balls are created fine, and drop perfectly well, but when they hit the Collector the Touched function doesn’t fire.

  3. What solutions have you tried so far?
    Tried reading through the BasePart.Touched page and the Detecting Collisions pages but I’m a bit confused since it doesn’t really explain why it isn’t working in my application.
    All the Parts involved have CanTouch true.
    The print("Collector hit") never prints in the Output window and there are no errors printed either.
    I’ve also tried making the first line and the 2 functions local but that didn’t help…

The script:

part1 = script.Parent

function ball(new) 
	prt.Name = "Ball"
	prt.Shape = 0
	prt.Material = "Neon"
	prt.CustomPhysicalProperties = PhysicalProperties.new(2, .2, 0, 1, 1)
	prt.Size = Vector3.new(4,4,4)
	prt.BrickColor = BrickColor.new(math.random(0,1), math.random(0,1),math.random(0,1))
	prt.Position = loc
	prt.AssemblyLinearVelocity = Vector3.new(0,-100,0)
	prt.CanCollide = true 
	prt.CanTouch = true
	prt.Parent = workspace
end

function ontouch(hit)
	print("Collector hit")
	if hit.Name == ("Ball") then 
		hit.Destroy()
	end
end

while true do
	prt = Instance.new("Part")
	loc = Vector3.new(math.random(-69.6,0.5),140,math.random(1065.2,1081.2))
	wait(3)
	ball()
	prt = Instance.new("Part")
	loc = Vector3.new(math.random(-141.7,-73.6),140,math.random(1065.2,1081.2))
	wait(3)
	ball()
	prt = Instance.new("Part")
	loc = Vector3.new(math.random(-213.8,-145.7),140,math.random(1065.2,1081.2))
	wait(3)
	ball()
end

part1.Touched:Connect (ontouch)

Explorer and Properties:

dropping balls

Thanks in advance for any help you can provide!

Move the touch event above the while loop.

It’s an infinite loop, so no code after it will run

2 Likes

You have a spelling mistake

You should write like this

hit:Destroy()

1 Like

You should do what he said too

Please try this! Its not THAT good but it might work for you. Tell me if you have any bugs

part1 = script.Parent
local loc = Vector3.new(0, 0, 0)
spawn(function()
	while true do
		local abc = 1
		local function ball()
			local prt = Instance.new("Part")
			if abc == 1 then
				loc = Vector3.new(math.random(-69.6,0.5),140,math.random(1065.2,1081.2))
			elseif abc == 2 then
				loc = Vector3.new(math.random(-141.7,-73.6),140,math.random(1065.2,1081.2))
			elseif abc == 3 then
				loc = Vector3.new(math.random(-213.8,-145.7),140,math.random(1065.2,1081.2))
			
			end
			prt.Name = "Ball"
			prt.Shape = 0
			prt.Material = "Neon"
			prt.CustomPhysicalProperties = PhysicalProperties.new(2, .2, 0, 1, 1)
			prt.Size = Vector3.new(4,4,4)
			prt.BrickColor = BrickColor.new(math.random(0,1), math.random(0,1),math.random(0,1))
			prt.Position = loc
			prt.AssemblyLinearVelocity = Vector3.new(0,-100,0)
			prt.CanCollide = true 
			prt.CanTouch = true
			prt.Parent = workspace
		end
		ball()
		wait(3)	
abc = 2
		ball()
		wait(3)
abc = 3
		ball()
	end
end)


function ontouch(hit)
	print("Collector hit")
	if hit.Name == "Ball" then 
		hit:Destroy()
	end
end



part1.Touched:Connect (ontouch)
1 Like

Thanks @Danielmasterlions and @WingItMan, I knew it was going to be something simple that I overlooked.
The Destroy wasn’t even being called so I never got any error message for it, but now I see that the touched event has to be above the while true loop.

Duh…

1 Like