Why won't a part detect when another part touches it?


The picture ^^ shows two parts, the ball and the actual part.
The ball’s script is:

script.Parent.Touched:Connect(function(lol)
	if lol.Name == "hit" then
		local ran4 = math.random(1,10)
		local ran6 = math.random(1,10)

		script.Parent.Velocity = Vector3.new(ran4,10,ran6)
	end
end)

And the part’s script is:

while true do
	script.Parent.Transparency = 1
	script.Parent.CanTouch = false
	script.Parent.CanCollide = false
	wait(5)
	script.Parent.Transparency = .5
	script.Parent.CanTouch = true
	script.Parent.CanCollide = true
	wait(1)
end

Two very simple scripts, but yet they don’t work. Thoughts?

edit: Part’s name is “hit” and the ball’s name is “ball”

Can you add this in the touched event to see if it shows up in the output?

print("touched")
script.Parent.Touched:Connect(function(lol)
	if lol.Parent.Name == "hit" then
		local ran4 = math.random(1,10)
		local ran6 = math.random(1,10)

		script.Parent.Velocity = Vector3.new(ran4,10,ran6)
	end
end)

Try that.

I assume both of the objects are anchored. When two parts are anchored .Touched wont fire because they basically don’t move(physically). Although there are functions that can be called manually to check two parts touching state, like workspace:GetPartsInPart(part1) and part1:GetTouchingParts() which return an array of parts touching the object.

local RunService = game:GetService("RunService")

function partsTouching(part1, part2)
	--local parts = workspace:GetPartsInPart(part1)
	local parts = part1:GetTouchingParts()
	if table.find(parts, part2) then 
		return true 
	else 
		return false 
	end
end

RunService.Heartbeat:Connect(function()
	local touching = partsTouching(workspace.Baseplate, workspace.SpawnLocation)
	print("touching:", touching)
end)

Although when it comes to anchored parts, those functions return true if the two parts are intersecting with eachother, not touching. To detect if two anchored parts are just touching, you may have to use raycasting.

2 Likes

I did that, and the script doesnt seem to register that the ball has touched it. Only the ball isn’t anchored, the part that is detecting whether the part has touched it or not. If it matters, the ball isn’t moving and is stuck between four parts. Not sure if that’s what is happening. But when I try and move the ball, the part doesn’t resigter that it touches the part, not a script, the actual part goes through the ball. I have no idea why

Only one of them is anchored, the part, not the ball. If it helps, the ball isn’t moving and is stuck, which is why I need to move it so its not stuck.

1 Like

If the ball doesn’t move, then the Touched event will not fire. The ball has to move and hit the floor, try positioning the ball higher.

I am making a ball throwing game if that makes sense, if the person misses, the ball will go down this sort of hole, I kind of want to avoid putting a part down each hole, since there is about 208 different holes. I have this part above all, which is on a while loop, enabling and disabling the script above. I know its probably not the most efficient, but I am a new scripter

edit: placed the parts in the holes, luckily they’re in a pattern so it was not painful at all

I’m glad you got it working. That would be time consuming if the holes weren’t in a pattern.