How to avoid breaking click detector after a variable is removed?

Hello! I’ve made this boxes, that inside them there’s a ClickDetector and a Script


that when the player click on it, it is destroyed, and the click detector of the other boxes are “disabled” and it able the player to place the boxes in the select location:
image
And to make it work again, when the player places the box, the ClickDetectors of the boxes that remain, are enabled again. By this script:

local box1 = game.Workspace.MissionsStuff.Chanching.BedBox

local box2 = game.Workspace.MissionsStuff.Chanching.DownstairsBox

local box3 = game.Workspace.MissionsStuff.Chanching.EntranceBox

local box4 = game.Workspace.MissionsStuff.Chanching.GarageBox

local box5 = game.Workspace.MissionsStuff.Chanching.KitchenBox

local box6 = game.Workspace.MissionsStuff.Chanching.OfficeBox1

local box7 = game.Workspace.MissionsStuff.Chanching.OfficeBox2

local box8 = game.Workspace.MissionsStuff.Chanching.RoomBox

function onClicked()

game.ReplicatedStorage.MissionStuff.Chanching.BoxesClosed.OfficeBox1:Clone().Parent = game.Workspace.MissionsStuff.Chanching

wait()

script.Parent:Destroy()

box1.ClickDetector.MaxActivationDistance = 4

box2.ClickDetector.MaxActivationDistance = 4

box3.ClickDetector.MaxActivationDistance = 4

box4.ClickDetector.MaxActivationDistance = 4

box5.ClickDetector.MaxActivationDistance = 4

box6.ClickDetector.MaxActivationDistance = 4

box7.ClickDetector.MaxActivationDistance = 4

box8.ClickDetector.MaxActivationDistance = 4

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

The problem here, is that one of the variables that I’ve created, doesn’t exist anymore. Because I’ve destroyed it. So how can I make the other click detectors start working again?

1 Like

You can save the variable in a table or a value.

2 Likes

You can use a for loop
Example:

local boxes = game.Workspace.BoxesFolder
for _, box in pairs(boxes:GetChildren()) do
if box.ClickDetector then
box.ClickDetector.Enabled = true
end
end
1 Like