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.
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.
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)
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.
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?
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)”
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.