Touch event pressure plate

I wanna make a pressure plate that works only when triggered with a certain part. I made the script but it keeps repeating the function. How do i make it so if its touched the function triggers once and then triggers something else when its not being touched? Is there an event for that?


local plate = game.Workspace.Pressure
local trigger = plate.Trigger
local gate = game.Workspace.Gate
local ts = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(
	9,
	Enum.EasingStyle.Cubic,
	Enum.EasingDirection.Out,
	0
)
local gatepos = gate.Position + Vector3.new(0,37,0)
local tween = ts:Create(gate, tweeninfo, {Position = gatepos})
trigger.Touched:Connect(function(hit)
	print(hit)
	if hit.Name == "Cube" then
		plate.plate.Position = plate.plate.Position - Vector3.new(0,0.1,0)
		plate.plate.Press.Playing = true
		wait(2)
		tween:Play()
	end
end)
2 Likes

and then triggers something else when its not being touched?

You mean you want something to happen when the part stops touching your pressure plate? For that you would use the TouchEnded event.

And if you only want it to run once, add a boolean that you set to false after the first touch ends.

local canRun = true

trigger.Touched:Connect(function(hit)
    if not canRun  then return end
	if hit.Name == "Cube" then
		--Whatever you want to happen when the touch starts
	end
end)

trigger.TouchEnded:Connect(function(hit)
    if not canRun  then return end
	if hit.Name == "Cube" then
		canRun = false
		--Whatever you want to happen when the touch ends
    end
end)

It doesnt solve the problem. The touch event happens multiple times at the same time. The object is supposed to be put on the plate by a player pushing it making the code run a lot of times.

Try setting the canRun variable to false in the .Touched instead:

local canRun = true
trigger.Touched:Connect(function(hit)
    if not canRun  then return end
	if hit.Name == "Cube" then
        canRun = false
		--Whatever you want to happen when the touch starts
	end
end)

Now the function should definitely only run once.

I don’t think you understand what i mean. I don’t want it to run ONCE in the entire game i mean that when the cube touches the plate the gate starts lifting up and the code runs once (pressure plate moving) and after that when the cube is off the plate it can be used again.

You can simply set canRun to true when you want it to be able to run again.

So you set canRun = false when the cube touches, and when the cube stops touching you set canRun = true so that it can run again.

local canRun = true

trigger.Touched:Connect(function(hit)
    if not canRun then return end

	if hit.Name == "Cube" then
		canRun = false
		--Open gate
	end
end)

trigger.TouchEnded:Connect(function(hit)
	if hit.Name == "Cube" and (canRun == false) then
		canRun = true
		--Whatever you want to happen when the touch ends
    end
end)

Still doesn’t work for some reason

What is the problem?
Is it still running a bunch of times at once? Is it running only once and never gain?..

It doesn’t run the touchended event

At all? Can you do this instead and add a print to check?

trigger.TouchEnded:Connect(function(hit)
	if hit.Name == "Cube" then
		print(canRun)
		if canRun == false then
			canRun = true
			--Whatever you want to happen when the touch ends
		end
    end
end)

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