How do i fix this really unreliable "pressure plate"

As you can see in the video, this button is pretty unreliable. Like sometimes i would step on it and it would activate, and then deactivate. And sometimes when i step off the button it would stay activated. How do i fix this, and make it more reliable? Also some feedback would be great too. Thanks!

BoolValueObject = script.Parent:WaitForChild("BoolValueObject")

Button = script.Parent.PrimaryPart
Sound = Button:WaitForChild("ButtonSound")

ButtonIncrement = 0.2

debounce = false
canToggle = true

function Activate()
	if not debounce and canToggle then
		debounce = true
		canToggle = false
		Button.Position = Button.Position - Vector3.new(0, ButtonIncrement, 0)
		local Bool = BoolValueObject.Value
		Sound:Play()
		--Bool.Value = true
		wait(0.2)
		canToggle = true
	end
end

function Deactivate()
	if debounce and canToggle then
		debounce = false
		canToggle = false
		Button.Position = Button.Position + Vector3.new(0, ButtonIncrement, 0)
		local Bool = BoolValueObject.Value
		Sound:Play()
		--Bool.Value = false
		wait(0.2)
		canToggle = true
	end
end

Button.Touched:Connect(Activate)
Button.TouchEnded:Connect(Deactivate)
1 Like

I’d say don’t add a deactivate script. Just make it so that the pressure plate turns off after like 5 seconds and you should be good to go.

1 Like

TouchEvent isn’t really the most reliable to go off of,

Consider using :GetTouchingParts()
hth!

1 Like

Try using Region3s instead of .Touched. It may be more reliable.

1 Like

Be sure to check out Instance | Documentation - Roblox Creator Hub. Humanoid floor type or whatever. Humanoid state (if they’re freefalling they won’t be on anything, so no need to check), and one RenderStep raycast from the HumanoidRootPart down tied to everything that might need it would seem to do the trick.

I’ve never actually tried this, but I theorized it.

Touch events are fired whenever something touches a part, then touch ended fires whenever something leaves the part.

Sometimes touch events don’t register all the touch events.


My theory to fix this problem is to not only check if the touched parts parent has a humanoid, but also check for a specific body part, for example, the right foot.

So the code would look something like this:

if hit.Parent:FindFirstChild("Humanoid") and hit.Name == "RightFoot" then

I’ve managed to fix the button by using :GetTouchingParts()

Its a bit hacky, but it does the trick!

BoolValueObject = script.Parent:WaitForChild("BoolValueObject")

Button = script.Parent.PrimaryPart
Sound = Button:WaitForChild("ButtonSound")

ButtonIncrement = 0.3

debounce = script.Parent:WaitForChild("debounce")

function Activate()
	if debounce.Value == false then
		debounce.Value = true
		Button.Position = Button.Position - Vector3.new(0, ButtonIncrement, 0)
		local Bool = BoolValueObject.Value
		Sound:Play()
		Bool.Value = true
	end
end

function Deactivate()
	if debounce.Value == true then
		debounce.Value = false
		Button.Position = Button.Position + Vector3.new(0, ButtonIncrement, 0)
		local Bool = BoolValueObject.Value
		Sound:Play()
		Bool.Value = false
	end
end

while true do
	wait()
	local function GetTouchingParts(part)
	    local connection = part.Touched:Connect(function() end)
	    local results = part:GetTouchingParts()
	    connection:Disconnect()
	    return results
	end
	
	local results = GetTouchingParts(Button)
	local count = 0
	
	for i, child in ipairs(results) do
		if child.Parent ~= script.Parent then
			count = count + 1
		end
	end
	
	
	
	print(count)
	if count > 0 then
		Activate()
	else
		Deactivate()
	end
end
1 Like