Detecting touch is very buggy

Hey, I made a big button and it can be triggered by a block touching it. Problem is touch and touchend is very very unreliable. Sometimes it doesn’t work and sometimes it does.

Here’s my current script

local RedPart = script.Parent
local TweenService = game:GetService("TweenService")
local First = RedPart.Parent.First
local Second = RedPart.Parent.Second
local Debounce = false

RedPart.Touched:Connect(function(BasePart)
	if BasePart:GetAttribute("Box") and not Debounce then
		Debounce = true
		RedPart.Triggered:Play()
		TweenService:Create(RedPart,TweenInfo.new(0.3,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out),{Position = Second.Position}):Play()
		print("Box has stepped on trigger.")
		wait(0.5)
		Debounce = false
	end
end)

RedPart.TouchEnded:Connect(function(BasePart)
	if BasePart:GetAttribute("Box") and not Debounce then
		Debounce = true
		RedPart.Triggered:Play()
		TweenService:Create(RedPart,TweenInfo.new(0.3,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out),{Position = First.Position}):Play()
		print("Box stopped touching on trigger")
		Debounce = false
	end
end)

What i’m looking for, is a method of detecting this accurately and more reliable.

I’ve searched for touch and touch end alternatives but they don’t work for this type of scenario

Are there gonna be more boxes which can touch the RedPart?
If not, remove debounce a cache actual box which is touching the part, then remove it when TouchEnded is called.

local Box = nil
RedPart.Touched:Connect(function(BasePart)
    if Box ~= nil or not BasePart:GetAttribute("Box") then
        return
    end
    RedPart.Triggered:Play()
    TweenService:Create(RedPart,TweenInfo.new(0.3,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out),{Position = Second.Position}):Play()
    print("Box has stepped on trigger.")
    Box = BasePart
end)

RedPart.TouchEnded:Connect(function(BasePart)
    if Box ~= BasePart then
        return
    end
    RedPart.Triggered:Play()
    TweenService:Create(RedPart,TweenInfo.new(0.3,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out),{Position = First.Position}):Play()
    print("Box stopped touching on trigger")
    Box = nil
end)

This is better approach since every other Touched call will be ignored since there is a box on it already, and every TouchEnded will be ignored if it’s not the box it wants.

1 Like

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