PressurePlate not responding after box removal

I’m having difficulty creating a system where a box is required to press a button. Some elements are already working, such as placing the box on top of the PressurePlate to trigger a function. However, when the box is removed, nothing happens, and I’m not sure why.

local PressurePlate = script.Parent
local Center = PressurePlate.Center

local BoxsName = "Box20A"

local TWPart = workspace.StonePassage20A

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

local Animation = TweenService:Create(TWPart, TInfo, Goal)

local BoxOnDebounce = false

--Make Passage
Center.Touched:Connect(function(hit)
	if hit:IsA("Part") and hit.Name == BoxsName and (not BoxOnDebounce) then
		BoxOnDebounce = true
		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)
				TweenService:Create(Center, TInfo, {CFrame = newCFrame}):Play()
				Center.CanTouch = false

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

						v.Color = NeonPartOn.Color
						NeonPartOn.Sound:Play()
						wait(0.05)
					end	
				end
			else
				print("not box")
			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)}
				TweenService:Create(v, TInfo, Goal):Play()

				wait(1)
			end
		end
	end

	Center.TouchEnded:Connect(function(hit)
		wait(1)
		if hit:IsA("Part") and hit.Name == BoxsName and BoxOnDebounce then
			BoxOnDebounce = false
			print("box is off")

			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()

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

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

3 Likes

required to press a button? is it like spawning one out of a wall with tweenservice or anything like that?

2 Likes

I didn’t quite understand what you meant with your question.

2 Likes

Just before you ask for an if check print the variables used in the check to see what’s being missed or not set correctly.

For example

print("x = ", x, "  y = ", y))
if x == y then
--code
2 Likes

i think it might be because you’re connecting the event after the pressure plate detects the block, i tried fixing your code up:

local PressurePlate = script.Parent
local Center = PressurePlate.Center

local BoxsName = "Box20A"

local TWPart = workspace.StonePassage20A

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

local Animation = TweenService:Create(TWPart, TInfo, Goal)

local BoxOnDebounce = false

--Make Passage
Center.Touched:Connect(function(hit)
	if not hit:IsA("Part") or hit.Name ~= BoxsName or BoxOnDebounce then return end
	-- removed indentation here*
	BoxOnDebounce = true
	print("box is on")

	--Turn Neon Thingy On
	task.defer(function()
		--[[ this is a bit nitpicky by my side, but task.defer is better than task.spawn
		in tasks that don't require IMMEDIATE action, since it's better for performance, 
		but they'll take slightly longer to run
		]]
		local NeonPartOn = {
			Color = Color3.fromRGB(255, 26, 26),
			Sound = PressurePlate.NeonParts:WaitForChild("NeonOnSound")
		}
		
		-- i removed a condition here because it was already being checked at the start of the script
		local newCFrame = Center.CFrame + Vector3.new(0, -0.25, 0)

		TweenService:Create(Center, TInfo, {CFrame = newCFrame}):Play()
		Center.CanTouch = false

		for _, v in PressurePlate.NeonParts:GetChildren() do
			if not v:IsA("Part") then continue end
			-- also removed indentation here**

			v.Color = NeonPartOn.Color
			NeonPartOn.Sound:Play()
			task.wait(0.05) -- task.wait() is superior than wait()***
		end	
	end)

		--Make Passage
	for _, v in TWPart:GetDescendants() do
		if not v:IsA("BasePart") then continue end

		local MovingStoneSound = v.MovingStoneSound
		MovingStoneSound:Play()

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

		task.wait(1)
	end
end)

Center.TouchEnded:Connect(function(hit)
	task.wait(1)
	if not hit:IsA("Part") or hit.Name ~= BoxsName or BoxOnDebounce then return end

	BoxOnDebounce = false
	print("box is off")

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

		local newCFrame = Center.CFrame + Vector3.new(0, -0.25, 0)
		TweenService:Create(Center, TInfo, {CFrame = newCFrame}):Play()

		for _, v in PressurePlate.NeonParts:GetChildren() do
			if not v:IsA("Part") then task.wait(1) continue end

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

* this script is very readable, and that’s good! :3 however, you should also avoid indentation to make it even more readable, an easy way to prevent this is to use returns in blocks that are only going to run if a condition is met:

local function checkIfCondition(condition)
	if condition then
		-- code here, don't do this pls
	end
end

--do this instead:
local function checkIfCondition(condition)
	if not condition then return end -- code inside this function stops executing here if the condition is not met, you have to invert the condition you want to check
	-- code here
end

**the continue statement is useful to skip an iteration in ANY loop, you should also use them to avoid indentation:

-- instead of:
for i = 1, 10 do
	if i ~= 5 then
		print(i) -- don't do this
	end
end

-- do this:
for i = 1, 10 do
	if i == 5 then continue end -- if i is equal to 5, skip this iteration and start again from the next one (6)
	print(i)
end

***pls don’t use wait(), it’s deprecated and it’s slower than its successor, task.wait(). it is recommended for you to use from now on task.wait() due to it being better, more info:

have a nice day :3

1 Like

I managed to fix the script myself after a moment. But in the same way, I appreciate the tips you gave me! Ty! I’m gonna for sure, use them C:

3 Likes