So here’s the thing. I was trying to fix this puzzle with pictures I have in my mascot horror game. The idea of the puzzle: There are three pictures, each has its own pair of eyes that can be moved INDIVIDUALLY.
The problem: They ARE NOT moving INDIVIDUALLY.
Here’s a recorded video of the problem
And here’s the code:
-----TWEENSERVICE&TWEENINFOS
local TweenService = game:GetService("TweenService")
local Tweeninfo = TweenInfo.new(0.2,
Enum.EasingStyle.Sine,
Enum.EasingDirection.InOut
)
-------Player-----------------
local cam = workspace.CurrentCamera
local value = false
--------------------CONTROLS-------------
local rightButton = script.Parent.Right
local leftButton = script.Parent.Left
local centreButton = script.Parent.Centre
for i, frame in pairs(game:GetService("CollectionService"):GetTagged("PictureLooks")) do
local ClickDetector = frame:FindFirstChild("ClickDetector")
local eyes = frame.Parent:FindFirstChild("eyes")
local center = frame.Parent:FindFirstChild("center")
local right = frame.Parent:FindFirstChild("right")
local left = frame.Parent:FindFirstChild("left")
local PictureLook = frame.Parent:FindFirstChild("PictureLook")
local function turnLeft ()
local leftTween = TweenService:Create(eyes, Tweeninfo, {CFrame = left.CFrame})
leftTween:Play()
end
local function turnRight ()
local rightTween = TweenService:Create(eyes, Tweeninfo, {CFrame = right.CFrame})
rightTween:Play()
end
ClickDetector.MouseClick:Connect(function()
if value == false then
value = true
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = PictureLook.CFrame
rightButton.Visible = true
leftButton.Visible = true
centreButton.Visible = true
rightButton.Activated:Connect(turnRight)
leftButton.Activated:Connect(turnLeft)
else
value = false
cam.CameraType = Enum.CameraType.Custom
end
end)
end
each time you select a frame, the connections for the last one aren’t getting disconnected, causing the buttons to replicate their actions on previously selected frames.
Here’s something to fix it:
local buttonConnections = {}
for _, v in buttonConnections do
v:Disconnect()
end
table.clear(buttonConnections)
table.insert(buttonConnections,
rightButton.MouseButton1Click:Connect(turnRight)
)
table.insert(buttonConnections,
leftButton.MouseButton1Click:Connect(turnLeft)
)
The table must be defined outside of the for loop, and just delete the button.Activated code and replace it with what I wrote.
local buttonConnections = {}
for _, v in buttonConnections do
v:Disconnect()
end
for i, frame in pairs(game:GetService("CollectionService"):GetTagged("PictureLooks")) do
local ClickDetector = frame:FindFirstChild("ClickDetector")
local eyes = frame.Parent:FindFirstChild("eyes")
local center = frame.Parent:FindFirstChild("center")
local right = frame.Parent:FindFirstChild("right")
local left = frame.Parent:FindFirstChild("left")
local color = frame.Parent:FindFirstChild("color")
local PictureLook = frame.Parent:FindFirstChild("PictureLook")
local function turnLeft ()
local leftTween = TweenService:Create(eyes, Tweeninfo, {CFrame = left.CFrame})
leftTween:Play()
end
local function turnRight ()
local rightTween = TweenService:Create(eyes, Tweeninfo, {CFrame = right.CFrame})
rightTween:Play()
end
ClickDetector.MouseClick:Connect(function()
if value == false then
value = true
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = PictureLook.CFrame
rightButton.Visible = true
leftButton.Visible = true
centreButton.Visible = true
table.insert(buttonConnections,
rightButton.MouseButton1Click:Connect(turnRight)
)
table.insert(buttonConnections,
leftButton.MouseButton1Click:Connect(turnLeft)
)
else
value = false
cam.CameraType = Enum.CameraType.Custom
rightButton.Visible = false
leftButton.Visible = false
centreButton.Visible = false
table.clear(buttonConnections)
end
end)
end
This is meant to be in the click detector mouse click function.
And this is meant to run after the code above.
clickDetector.MouseClick:Connect(function()
if not value then
for _, v in buttonConnections do
v:Disconnect()
end
table.clear(buttonConnections)
table.insert(buttonConnections,
rightButton.MouseButton1Click:Connect(turnRight)
)
table.insert(buttonConnections,
leftButton.MouseButton1Click:Connect(turnLeft)
)
else
--// bla bla
end
end)