Script Breaking Due to Collision Issues in the Pressure Plate System

A bug is occurring with me, with a pressure plate system that I am making. Basically, sometimes when I place the box, or when I move between it, it causes a bug (apparently the collision), and causes the entire script to break. I even tried to put a collision group to make it impossible for the box to move, but that didn’t solve it.

Here’s the Pressure Plate Script:

Pressure Plate Script
local PressurePlate = script.Parent

local hitBox = PressurePlate.Hitbox
local Center = PressurePlate.Center

local BoxsName = "Box20A"
local BoxInWorkspace
local BoxPrompt

local TWPart = workspace.StonePassage20A

local TweenService = game:GetService("TweenService")
local TInfo = TweenInfo.new(1.8, Enum.EasingStyle.Circular, Enum.EasingDirection.Out, 0, false, 0)
local Goal = {}

local CenterAnimation
local PassageAnimation

local BoxOnDebounce = false

--Make Passage
hitBox.Touched:Connect(function(hit)
	if hit:IsA("Part") and hit.Name == BoxsName and (not BoxOnDebounce) then

		BoxOnDebounce = true

		BoxInWorkspace = workspace.Boxes[BoxsName]
		BoxPrompt = BoxInWorkspace.Attachment.ProximityPrompt

		BoxPrompt.Enabled = false

		print("box is on")

		--Turn Neon Thingy On
		task.spawn(function()
			local NeonPartOn = {
				Color = Color3.fromRGB(255, 26, 26),
				Sound = PressurePlate.NeonParts:WaitForChild("NeonOnSound")
			}

			if hit:IsA("Part") and hit.Name == BoxsName then
				local newCFrame = Center.CFrame + Vector3.new(0, -0.25, 0)
				CenterAnimation = TweenService:Create(Center, TInfo, {CFrame = newCFrame})
				--CenterAnimation:Play()

				task.spawn(function()
					wait(6) -- Total Wait Time
					BoxPrompt.Enabled = true
				end)

				for _, v in PressurePlate.NeonParts:GetChildren() do
					if v:IsA("Part") then

						v.Color = NeonPartOn.Color
						NeonPartOn.Sound:Play()
						wait(0.05)
					end	
				end
			end
		end)

		--Make Passage
		for _, v in pairs(TWPart:GetDescendants()) do
			if v:IsA("Part") or v:IsA("UnionOperation") then

				local MovingStoneSound = v.MovingStoneSound
				MovingStoneSound:Play()

				Goal = {Position = v.Position + Vector3.new(0, 13, 0)}
				PassageAnimation = TweenService:Create(v, TInfo, Goal)
				PassageAnimation:Play()

				wait(1)
			end
		end
	end
end)

hitBox.TouchEnded:Connect(function(hit)
	if hit:IsA("Part") and hit.Name == BoxsName and BoxOnDebounce then

		BoxOnDebounce = false
		BoxPrompt.Enabled = false

		task.spawn(function()
			local NeonPartOff = {
				Color = Color3.fromRGB(8, 8, 8),
				Sound = PressurePlate.NeonParts:WaitForChild("NeonOnSound")
			}

			if hit:IsA("Part") and hit.Name == BoxsName then
				local newCFrame = Center.CFrame + Vector3.new(0, 0.25, 0)
			--	TweenService:Create(Center, TInfo, {CFrame = newCFrame}):Play()

				task.spawn(function()
					wait(6) -- Total Wait Time
					BoxPrompt.Enabled = true
				end)

				for _, v in PressurePlate.NeonParts:GetChildren() do
					if v:IsA("Part") then

						v.Color = NeonPartOff.Color
						NeonPartOff.Sound:Play()
						wait(0.05)
					end
				end	

				--Leave Passage
				for _, v in pairs(TWPart:GetDescendants()) do
					if v:IsA("Part") or v:IsA("UnionOperation") then

						local MovingStoneSound = v.MovingStoneSound
						MovingStoneSound:Play()

						Goal = {CFrame = v.CFrame + Vector3.new(0, -13, 0)}
						TweenService:Create(v, TInfo, Goal):Play()

						wait(1)


					end
				end
			end
		end)
	end
end)

And the Box Script:

Box Script
local grabbed = false
local BoxAlreadyExists = true

local Prompt = script.Parent.Attachment.ProximityPrompt

local ServerStorage = game:GetService("ServerStorage")
local BoxesFolder = workspace:WaitForChild("Boxes")
local Box = script.Parent

local BoxClone = Box:Clone()

local weld

Prompt.Triggered:Connect(function(player)
	local Human = player.Character.HumanoidRootPart
	local x = Human.CFrame.X
	local y = Human.CFrame.Y - 1
	local z = Human.CFrame.Z
	
	if grabbed then
		Human.Weld:Destroy()
		Box.CanCollide = true
		Box.CFrame = CFrame.new(x, y, z)
		Box.CFrame = Box.CFrame + Human.CFrame.LookVector * 4
		Box.Orientation = Human.Orientation
		Box.Parent = BoxesFolder


		Box.Massless = false
		Box.CanTouch = true

		grabbed = false

		Prompt.ActionText = "Pick up"

		wait(0.5)

	elseif not grabbed then
		Box.Parent = workspace:WaitForChild("Boxes")
		Box.CFrame = CFrame.new(x, y, z)
		Box.CFrame = Box.CFrame + Human.CFrame.LookVector * 4
		Box.Orientation = Human.Orientation
		Box.CanCollide = false

		Box.Massless = true
		Box.CanTouch = false

		weld = Instance.new("WeldConstraint", Human)
		weld.Part0 = Human
		weld.Part1 = Box
		weld.Name = "Weld"

		grabbed = true

		Prompt.ActionText = "Drop"

		wait(0.5)
	end
end)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.PlayerDiedWithBox.PlayerDiedWithBoxRemote

RemoteEvent.OnServerEvent:Connect(function(player)
	if grabbed then
		Prompt.MaxActivationDistance = 0
		Box.CanCollide = true
		Box.Massless = false
		Prompt.ActionText = "Pick up"
		grabbed = false		

		wait(3)

		Prompt.MaxActivationDistance = 10
	end
end)

If possible, be able to find a way around the problem, or do something different. Because I don’t know what else to do

2 Likes

What else have you tried?
Copy and paste the script into a test game because it might be the game

2 Likes

Before, there was an animation of the center of the pressure plate, which descended a little. At first, I thought this would be the problem. But it wasn’t (in the script it’s even visible that I left some Tweens as comments).
I tried putting a collision group just for the box, which also didn’t work. And I tried to make it so that when the box is dropped, it is anchored, which also doesn’t solve the problem, and which would create other bugs

Maybe Region3 is the solution if the problem is the Touched event

2 Likes

are you using something other than a part for the hitbox ?

this could also be a solution

but i feel like its detecting the player instead of the box and therefore keeping the pressure plate up when the player interferes

try something like

if hit == Character then return end
3 Likes

There is no way the script is detecting the player instead of the box, because of this part:

	if hit:IsA("Part") and hit.Name == BoxsName and (not BoxOnDebounce) then

Region3 I’ve never heard of it before, and I don’t really know what the difference is. Would it be possible to give a summary and how could I put it in the script?

1 Like

I extremely recommend Region3 above Touched event.
This is a guide if you want to know more :smiley_cat:

1 Like

How about anchoring the box when placed and then unanchoring when the player wants to pick it back up?

And lets say you dont want that, then make a fake box that you can push around but the real box is still on the pressure plate, then you constantly check how far the fake box is from the plate and then based on the distance you say if its still on the plate. You can do this with collision group.

1 Like

Im just going to throw something in the ring here. Is it possible something wasnt properly reset when the box was removed?

It looks as if it thinks the box is both on it and off it over and over again, and you do have a few loops in your code.

Oh wait ignore that. Touching it breaks it?!

1 Like

Touch is sometimes hard to work with. If the part touches and then stops moving while still on the touch part, it can reset everything. When the part moves again, it touches again, disrupting the whole start and end process. From the looks of your video, this seems to be what’s happening.

Try using more than one touched part or possibly a distance check.
Need to have two points of reference here so that isn’t a problem. You are holding it when it’s not pushing down the plate …

If touched the plate, it’s active.
If after that … if you are holding it, it’s not active and/or
it touched a plate that is around the other plate, it’s no longer active.

This is tricky as you can’t ask, are you still touching. You could also make an area and test if it’s within that area as a 2nd reference. I’ve seen this fixed doing it that way also.

Here is a simple test showing what is going on with Touched and TouchEnded.
Make a flat Part on the workspace with this script in it. Walk on the part and stop…
Then start moving again…

local part = workspace.Part

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		print("Player is touching the part.")
	end
end)

part.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		print("Player is no longer touching the part.")
	end
end)

Few things you can try here.

Instead of hit:IsA(“Part”), try hit:IsA(“BasePart”)
I know there is practically no difference, but worth a shot.

Try removing the DeBounce, on both Touched and TouchedEnded. Sounds strange, but since this is also relying on a DeBounce value to run it, it could possibly be conflicting as Touch and TouchEnded don’t wait for each other, so it will run individually, and if a part or something is constantly moving in and out of the part, it can cause this.

Another thing, could you show a screenshot of this box in Explorer? Is it just a single part of a part inside of a Group?