I am trying to make a system where if the green cube is on the Green Plate and the red cube is on the Red Plate it opens the doors. When the cubes are on the right plate the counter text is not changed and the doors don’t open. I am decently new to scripting so I have not found a reason for the issue, help would be appreciated. Here is the code –
local TweenService = game:GetService("TweenService")
local folder = script.Parent.Parent
local platePart1 = script.Parent.RedPlate:WaitForChild("Collision")
local platePart2 = script.Parent.GreenPlate:WaitForChild("Collision")
local counterLabel = folder.Counter.Screen:WaitForChild("SurfaceGui"):WaitForChild("TextLabel")
local elevator = folder.Exit.Door
local Right = elevator:WaitForChild("Right")
local Left = elevator:WaitForChild("Left")
local rightPos = Right.Position
local leftPos = Left.Position
local RedCube = folder.Redcube
local GreenCube = folder.Greencube
local counterState = 0
local plateActivated = {
[platePart1] = false,
[platePart2] = false
}
local originalMaterials = {
[platePart1] = platePart1.Material,
[platePart2] = platePart2.Material
}
local debounce = {
[platePart1] = false,
[platePart2] = false
}
local function movePlateDown(plate)
local targetPosition = plate.Position - Vector3.new(0, 0.1, 0)
if plate.Position.Y > targetPosition.Y then
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local goal = {Position = targetPosition}
local plateTween = TweenService:Create(plate, tweenInfo, goal)
plateTween:Play()
end
end
local function activatePlate(plate)
plate.Material = Enum.Material.SmoothPlastic
movePlateDown(plate)
end
local function openDoors()
local rightGoal = rightPos - Vector3.new(3, 0, 0)
local leftGoal = leftPos + Vector3.new(3, 0, 0)
local rightTween = TweenService:Create(Right, TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = rightGoal})
local leftTween = TweenService:Create(Left, TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = leftGoal})
rightTween:Play()
leftTween:Play()
end
local function updateCounter()
if counterState == 0 and plateActivated[platePart1] and plateActivated[platePart2] then
counterLabel.Text = "1/2"
counterState = 1
elseif counterState == 1 and plateActivated[platePart1] and plateActivated[platePart2] then
counterLabel.Text = "2/2"
counterState = 2
openDoors()
elseif counterState > 0 and (not plateActivated[platePart1] or not plateActivated[platePart2]) then
counterLabel.Text = "0/2"
counterState = 0
end
end
local function onTouched(hit, plate)
if hit:IsA("BasePart") and (hit == RedCube or hit == GreenCube) and not debounce[plate] then
debounce[plate] = true
if plate == platePart1 and hit == RedCube then
activatePlate(plate)
plateActivated[plate] = true
updateCounter()
elseif plate == platePart2 and hit == GreenCube then
activatePlate(plate)
plateActivated[plate] = true
updateCounter()
end
wait(1) -- debounce
debounce[plate] = false
end
end
local function onTouchEnded(hit, plate)
if hit:IsA("BasePart") and (hit == RedCube or hit == GreenCube) then
if plate == platePart1 and hit == RedCube then
plateActivated[plate] = false
plate.Material = originalMaterials[plate]
updateCounter()
elseif plate == platePart2 and hit == GreenCube then
plateActivated[plate] = false
plate.Material = originalMaterials[plate]
updateCounter()
end
end
end
platePart1.Touched:Connect(function(hit) onTouched(hit, platePart1) end)
platePart2.Touched:Connect(function(hit) onTouched(hit, platePart2) end)
platePart1.TouchEnded:Connect(function(hit) onTouchEnded(hit, platePart1) end)
platePart2.TouchEnded:Connect(function(hit) onTouchEnded(hit, platePart2) end)