Having trouble with

I’m using a part to detect…

This has been resolved.

I would recommend using Region3s rather than the .Touched event.

2 Likes

I would definetly use something else other than the Touched event, because the trigger might not be touching it after 3 seconds or so, I would use something more reliable.

1 Like

This has been resolved. This has been resolved. This has been resolved.

1 Like

You may find this helpful

It’s fairly similar to your issue, however it would require a different take on the If statement.

Part.Touched:Connect(function(Hit)
   local Hits = Part:GetTouchingParts()
   for _, v in ipairs(Hits) do
      if v.Name == "Trigger" then
         --do something
      end
   end
end)

Part.TouchEnded:Connect(function(Hit)
   local Hits = Part:GetTouchingParts()
   if #Hits == 0 or Hit.Name == "Trigger" then
      --do something
   end
end)

You probably get what I’m trying to pull off here, although it mitigates the issue to an extent, the method that I would consider the best would be using Region3s.

2 Likes

Thank you, I will try this now!

1 Like

This has been resolved. This has been resolved. This has been resolved.

The code I posted was just an example, you would have to modify it to fit your code accordingly.

This has been resolved. This has been resolved. This has been resolved.

I read your post again and I believe the issue could be a lack of a debounce? Anyway, try this code, I missed a debounce in my code as well.

local Part = script.Parent
local Debounce = false

Part.Touched:Connect(function(Hit)
	local Hits = Part:GetTouchingParts()
	for _, v in ipairs(Hits) do
		if v.Name == "Trigger" and not Debounce then
			Debounce = true
			--do something
		end
	end
end)

Part.TouchEnded:Connect(function(Hit)
	local Hits = Part:GetTouchingParts()
	if #Hits == 0 or Hit.Name == "Trigger" then
		if Debounce then
			Debounce = false
			--do something
		end
	end
end)

Edit:
A fix for your original code could be this:

local Part = script.Parent
local Debounce = false

Part.Touched:Connect(function(Hit)
	if not Debounce then
		if Hit.Name == "Trigger" then
			Debounce = true
			--do something
		end
	end
end)

Part.TouchEnded:Connect(function(Hit)
	if Debounce then
		if Hit.Name == "Trigger" then
			Debounce = false
			--do something
		end
	end
end)

and this would probably work better. The code I suggested is intended for a different purpose.

2 Likes