Touch event acting weird

Good day, I have this pressure plate that goes down when stepped on, and up when it’s released and plays a sound when it happens, the issue being that it usually plays the sound at least twice and sometimes the tween animation resets or doesn’t play at all.

It has:
The plate itself, which is the neon part.
A Hitbox, which is the red Forcefield.
And the Start and Goal parts which are invisible.

the script is also fairly simple:

HB.Touched:Connect(function()
		downTween:Play()
		TriggerSound:Play()
		Door.CanCollide = false
		Door.Transparency = 1
	end
end)

HB.TouchEnded:Connect(function()
		upTween:Play()
		TriggerSound:Play()	
		Door.CanCollide = true
		Door.Transparency = 0
	end
end)

What’s funny is that the sound and tween don’t work as intended but the CanCollide and Transparency part work fine, the door stays the way I want it and I want to know why it works with the door but not with the sound and tween.
And even worse, I have a second plate with the exact same things but on that one it works much better, usually only playing the sound twice at the start but it’s barely noticeable.

2 Likes

Try this

HB.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and HB.Transparency == 0 then -- makes sure it's a player that touched it.
		downTween:Play()
		TriggerSound:Play()
		Door.CanCollide = false
		Door.Transparency = 1
	end
end
end)

HB.TouchEnded:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and HB.Transparency == 1 then
		upTween:Play()
		TriggerSound:Play()	
		Door.CanCollide = true
		Door.Transparency = 0
	end
end
end)

Also add a debounce if you want

1 Like

I got the same results, it doesn’t make any sense either, I’m standing still and a second later I hear the sound again, hit detections in Roblox are really weird.

1 Like

You need to check what is touching the pressure plate. If another part touches the pressure plate, it will still activate. That’s the idea behind your issue. The code should work, but you can still do your own research. Also, why is there an additional end at the end of each event?

1 Like

I edited the code a little you should try it again

Mhm, that would make sense, but how come it works better on another one that has literally the same stuff?

1 Like

How is it any better on the other one?

Well, it usually only plays the sound twice at the beginning, if I put prints on this one it shows as
“pressed
not pressed
pressed
not pressed (x2)
pressed (x2)
not pressed”

while on the other one it just says
“pressed (x2)”

1 Like

And regarding your question about why there was a 2nd end, I honestly have 0 idea, I don’t remember typing that, I probably didn’t see the first end or something.

Touched events are inconsistent to use, and isn’t reliable - it may even fire the function multiple times instead of once. You could possibly try using RenderStepped and check the distance using .Magnitude (if they’re close enough) and commit that function (with a debounce). There may possibly be other ways, but that’s what I can acknowledge. An example of using RenderStepped would be:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local rs = game:GetService("RunService")
rs.RenderStepped:Connect(function()
	local toCompare = (char:WaitForChild("HumanoidRootPart").Position - game.Workspace.Test.Position).Magnitude
	if toCompare <= 3 then
		plr.PlayerGui.ScreenGui.Frame.Visible = true
	else
		plr.PlayerGui.ScreenGui.Frame.Visible = false
	end
end)
--localscript in backpack

RenderStepped can only be run in localscripts, so you’d need to fire remote events for everyone else to see the platforms moving. You could also potentially make a table to ensure that the platforms won’t go to the wrong direction.

2 Likes