So for one of the puzzles in my game i need a pressure plate.
But when i made one it wasn’t working, when i made it print out “Touched” and “TouchEnded” i saw that they followed after eachother very quickly, but i need an event that runs when the part has fully left the plate’s body.
Do you know how to fix this or an event which i can use?
Script In the pressure plate:
local Part = script.Parent
local LastHit = nil
local TweenService = game:GetService("TweenService")
local Tweeninfo = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
Part.Touched:Connect(function(hit)
Part:SetAttribute("SignalON",true)
LastHit = hit
TweenService:Create(Part,Tweeninfo,{Position = Part.Position - Vector3.new(0,2,0)}):Play()
print("Touched")
end)
Part.TouchEnded:Connect(function(hit)
if not LastHit then return end
if LastHit ~= hit then return end
Part:SetAttribute("SignalON",false)
TweenService:Create(Part,Tweeninfo,{Position = Part.Position + Vector3.new(0,2,0)}):Play()
print("TouchEnded")
end)
The problem is that a player’s character has many body parts, and it runs every time any of the body parts touches or stops touching the plate. The character is a bit “shaky” and it’s possible that when you walk around, small body parts touch and stop touching very quickly.
I would fix this by having a non-collidable invisible box above the plate, and checking if it is touched by the player’s HumanoidRootPart specifically.
(Just return if the part that is touching isn’t equal to the HumanoidRootPart)
local Part = script.Parent
local TweenService = game:GetService("TweenService")
local lastHit = nil
local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
Part:SetAttribute("SignalON", false)
Part.Touched:Connect(function(hit)
if not Part.SignalON then
Part:SetAttribute("SignalON", true)
lastHit = hit
TweenService:Create(Part, tweenInfo, {Position = Part.Position - Vector3.new(0,2,0)}):Play()
print("Touched")
end
end)
Part.TouchEnded:Connect(function(hit)
if hit.Parent == lastHit.Parent and Part.SignalON == true then
Part:SetAttribute("SignalON", false)
TweenService:Create(Part, tweenInfo, {Position = Part.Position + Vector3.new(0,2,0)}):Play()
print("TouchEnded")
end
end)
One of the body parts for example LeftFoot touched it, but then it left triggering the TouchEnded. But another part like the RightFoot might’ve stayed in.
The Touched and TouchEnded events should have a debounce in them with a task.wait(some time, depends on how sensitive you want it) to get rid of multiple firings.
Precisely.
I searched debounce on their documentation site and it used to have a link, but it doesn’t show anymore for some reason. A quick google search found it though: Debounce Patterns | Documentation - Roblox Creator Hub
You seem to have a SignalOn boolean, and lastHit = Hit, but a boolean made true and false in each of the functions with a task.wait(2) or whatever time works for you.
local Part = script.Parent
local TweenService = game:GetService("TweenService")
local lastHit = nil
local CanFire = true
local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
Part:SetAttribute("SignalON", false)
Part.Touched:Connect(function(hit)
if not CanFire then return end
if not Part:GetAttribute("SignalON") then
Part:SetAttribute("SignalON", true)
CanFire = false
lastHit = hit
local twen = TweenService:Create(Part, tweenInfo, {Position = Part.Position - Vector3.new(0,2,0)})
twen:Play()
repeat wait() until twen.PlaybackState == Enum.PlaybackState.Completed
CanFire = true
end
end)
Part.TouchEnded:Connect(function(hit)
if not CanFire then return end
if not lastHit then return end
if hit.Parent == lastHit.Parent and Part:GetAttribute("SignalON") == true then
Part:SetAttribute("SignalON", false)
CanFire = false
local twen = TweenService:Create(Part, tweenInfo, {Position = Part.Position + Vector3.new(0,2,0)})
twen:Play()
repeat wait() until twen.PlaybackState == Enum.PlaybackState.Completed
CanFire = true
end
end)
task.wait(1) -- since that's the length of your tween, you may want to increase it to keep the touched event from firing before you're ready for it.
CanFire = true
just before the end of the if not CanFire statementsto keep them from firing each and every time the Touched or TouchEnded events fire with the Humanoid. If you set it to true right away then the next Touched or Touchended will fire and run the tween again.
Are the SignalOn, lastHit, and Hit all attempts to get this issue to stop? If so you can get rid of all of them and just have the CanFire debounce.