Pressure Plate Problem

I want to make a reliable pressure plate that continues to detect players that stand still, but I don’t want
to use Region3 because it’s inaccurate (4x4x4 grid).

Im experiencing big lag spikes while stepping on the pressure plate and the game doesn’t detect the moment when I leave the pressure plate correctly.

I’ve tried changing up the script quite a bit, but nothing worked so far.

local RS = game:GetService("RunService")

local function activate(act,acte)
	for i, c in pairs(act:GetTouchingParts()) do
		if c.Parent:FindFirstChild("Humanoid") then
			acte.Transparency = 0.7
			acte.CanCollide = false
		elseif c.Parent:FindFirstChild("Humanoid") == nil then
			acte.Transparency = 0
			acte.CanCollide = true
		end
	end
end

while RS.Heartbeat:Wait() do
	for i, b in pairs(workspace.Buttons:GetChildren()) do
		for i, v in pairs(b:GetChildren()) do
			if v.Name == "Activator" then
				local g = v.Parent.Activatee
				v.Touched:Connect(function(start)
				activate(v,g)
				end)
			end
		end
	end
end

It’s lagging because you’re making a new Touched connection every 1/60th of a second, and each time the button is touched, all of the touched events are fired.

4 Likes

I actually made a post on something similar, hopefully it helps:

Letme know if you have any questions, whether that be about implementing the above in your case or something else.

2 Likes

My friend told me about this but said it would be useless. Imma try it either way.

That what would be useless?

By the way, from what I’m seeing you have a pressure plate that, when activated, should change the Transparency of your wall part to 0.7 and set CanCollide to false, and when disactivated set the Transparency to 0 and CanCollide to true. If that is the case, then implementation with the method in my post I linked should end up looking something like this:

local function GetTouchingParts(part)
   local connection = part.Touched:Connect(function() end)
   local results = part:GetTouchingParts()
   connection:Disconnect()
   return results
end

local myPart = -- Path in workspace to the part to be checked

while true do
    local touchingParts = GetTouchingParts(myPart) -- Returns a table with all touching parts
    
    for i, v in pairs(touchingParts) do
        if v.Parent:FindFirstChild("Humanoid") then
            myPart.Transparency = 0.7
            myPart.CanCollide = false
        else
            myPart.Transparency = 0
            myPart.CanCollide = true
        end
    end
end
4 Likes

I removed the connection and now the script looks like this:

while wait() do
	for i, b in pairs(workspace.Buttons:GetChildren()) do
		for i, v in pairs(b:GetChildren()) do
			if v.Name == "Activator" then
				local g = v.Parent.Activatee
				v.Touched:Connect(function(start)
				--activate(v,g)
					for i, c in pairs(v:GetTouchingParts()) do
						if c.Parent:FindFirstChild("Humanoid") then
							g.Transparency = 0.7
							g.CanCollide = false
						elseif c.Parent:FindFirstChild("Humanoid") == nil then
							g.Transparency = 0
							g.CanCollide = true
						end
					end
				end)
			end
		end
	end
end

Still don’t know what to do about the checks tho.

With some slight tweaks it worked! Thanks!

1 Like

There is one problem that I didin’t adress in the post. I want the script to work with all doors. (The script I made did that.)

I’m pretty sure that’s not the case

That code seems to create a memory leak.
image

Loop-creating an event connection like that isn’t a good idea.

Also, using .Touched is kind of finicky from my experience.
Here’s a pressureplate system I made a while back. I ended up using .Magnitude, but the code for how you’d theoretically use .Touched is in there, just commented out.

This has already been resolved. Thanks for the comment though!

Please mark the most appropriate post as the solution, so other devforum helpers don’t have to click this and figure out the problem all over again. It also helps other devforum users out there trying to find a solution to this similar problem, thanks

Nothing in here really was a solution but ok.