Problem with .Touched only firing if the other part is moving

Hello and I have problems with .Touched event, It doesn’t work if the other part isn’t moving, It looks like this.

3 Likes

That is because .Touched only gets fired when there is physical movement. In Roblox Studio, it reads quote:

“Fires when a part touches another part as a result of physical movement.”

That means when there is no movement, it does not get fired. If you want something to fire while touching, you can run a while loop in a .Touched, and then disable the while loop in a .TouchEnded event.


Edit: I made a quick over-complicated mockup to show in summary what I mean. Note that this is a ServerScript as the child of the part that will run the event when touched. It currently checks just for touching BaseParts.

local part = script.Parent

local isTouching = false
local debounce = false

part.Touched:Connect(function(hit)
	
	debounce = true
	isTouching = true
	
	if hit:IsA("BasePart") then
		debounce = true
		print("Touching part.")
		while isTouching == true do
			task.wait(0.1) 
			debounce = false
            --//Execute your function here.
		end
	end
	
	print("Nevermind.")
	
end)

part.TouchEnded:Connect(function()
	
	isTouching = false
	
end)
2 Likes

Touched is very unreliable in this case (like the person above said), you should probably use workspace:GetPartsInPart() instead

local part — part
while true do — loop
task.wait(0.1)
for i, v in workspace:GetPartsInPart(part) do — loop through parts touching the part
if v.Name == “partname” then — check if the part is touching another part with specified name
— touching
end
end
end
2 Likes

You can try using:

part:GetTouchingParts() -- {}

It works, but it doesn’t work with CanCollide off parts, how can I make it work in non collidable parts?

1 Like

Use GetPartsInPart instead, it works on both collidible and non-collidible parts

1 Like

I don’t know how to use OverlapParams.

2 Likes

useful documentation about spatial queeries:

1 Like

be warned though- I’m fairly certain that if you set off CanQueery to false, it will disable spatial queeries for that part.

1 Like

You don’t need one in this case, it’s just optional to use it to filter out parts. If you want, use the documentation that @SkrubDaNub posted

1 Like

It worked! Thank you so much!!!

1 Like

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