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?
I’ve tried using what I think is a table:

local clickDetectors = {};

table.insert(clickDetectors, game.Workspace.MissionsStuff.Chanching.BedBox.ClickDetector)

table.insert(clickDetectors, game.Workspace.MissionsStuff.Chanching.DownstairsBox.ClickDetector)

table.insert(clickDetectors, game.Workspace.MissionsStuff.Chanching.EntranceBox.ClickDetector)

table.insert(clickDetectors, game.Workspace.MissionsStuff.Chanching.GarageBox.ClickDetector)

table.insert(clickDetectors, game.Workspace.MissionsStuff.Chanching.KitchenBox.ClickDetector)

table.insert(clickDetectors, game.Workspace.MissionsStuff.Chanching.OfficeBox1.ClickDetector)

table.insert(clickDetectors, game.Workspace.MissionsStuff.Chanching.OfficeBox2.ClickDetector)

table.insert(clickDetectors, game.Workspace.MissionsStuff.Chanching.RoomBox.ClickDetector)

function onClicked()

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

wait()

script.Parent:Destroy()

clickDetectors.MaxActivationDistance = 4

end

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

But the script still brokes.

Jeez thats alot of table.insert spam

1 Like

I don’t usually work wit tables when it comes to parts. I only do it with ui’s but I’ll try to help

Yeah, I know. I really don’t know how to do this. All my codes are a mess, but I can find myself on it.

You should look at using CollectionService | Roblox Creator Documentation, as it was designed for managing groups of objects at a time. Collections are identified with tags, so you’ll benefit from using a tag editor plugin like this.

It’ll take a little messing around with it to understand how they work, but it helps in the long run.

Thanks for not giving a done script!

1 Like

For the first script you posted you could just do

if box1 then
 box1.ClickDetector.MaxActivationDistance
end

etc.

Also, scripting support was not made for people to give each other people full scripts it was made to teach people how to write those scripts.

Yes, that’s why I marked the guy that gave me the direction to learn it as the Solution. Because that was would help me to learn.

I was replying because you said

This made it seem like you were expecting them to write the full script for you.

I actually was. 90% of the users send full scripts when I have a quote. Even when I specify that I don’t want that,

1 Like