Attempt to get length of a Instance value

Hello! I’m trying to make a script that “disable” the click detector of the remaining objects in a folder in workspace, but the problem is that i get this error. I can’t use a table, as some variable would be missing, because some objects could be destroyed.
btw, here’s the script:

local otherBoxes = game:GetService("Workspace").CurrentMission

local debounce = false

function click()

if debounce then return end

debounce = true

game:GetService("Workspace").Trashes.TrashSelection1.ClickDetector.MaxActivationDistance = 7

game:GetService("Workspace").Trashes.TrashSelection1.Script.Disabled = false

--

for i = 1, #otherBoxes do

otherBoxes[i].Box.DeleteBox.MaxActivationDistance = 0

end

--

wait(0.1)

script.Parent.Parent:Destroy()

end

script.Parent.DeleteBox.MouseClick:Connect(click)

I think you want to get the children in what I assume is the folder CurrentMission, so…

local OtherBoxes = game.Workspace.CurrentMission:GetChildren()

also neat little thing with for loops

for i, box in ipairs(otherBoxes) do
    box.Box.DeleteBox.MaxActivationDistance = 0
end

There’s one problem image

I’m not sure what’s wrong in that image sorry? Can you explain what the problem is?

The script still brokes because one of the variables was removed… I though if I called the children it would not happen… but it looks like i was wrong
image

Which line is this happening on? it doesn’t help to know the error message if I don’t know which line is the problem.

Looking at this line, I accidentally deleted the .Box, my bad

box.Box.DeleteBox.MaxActivationDistance = 0

After removing the box from that script, it activates another scripts that should re-activates the remaining click detectors, but it doesn’t happens and the script brokes because I have deleted one of them before…
Here’s the script that when you click another click detector activates the click detector that were disabled:

local children = game:GetService("Workspace").CurrentMission:GetChildren()
local debounce = false

function click()
	if debounce then return end
	debounce = true
	game:GetService("ReplicatedStorage").comerBoxes.box1:Clone().Parent = game:GetService("ReplicatedStorage").trashBoxes
	wait()
	game:GetService("ReplicatedStorage").comerBoxes.box1:Destroy()
	
	for i = 1, #children do
		children[i].Box.DeleteBox.MaxActivationDistance = 8
	end
	wait(0.1)
	script.Parent:Destroy()
	
end

script.Parent.ClickDetector.MouseClick:Connect(click)

This might solve it idk.

for i = 1, #otherBoxes do
   if otherBoxes[i]:FindFirstChild("Box") then
       otherBoxes[i].Box.DeleteBox.MaxActivationDistance = 0
   end
end

The real problem is, I though that by calling the child, I would have no problem with missing variables, but looks like this is a thing…

nvm, It’s working, but now I’ve got another problem…
image
It’s with the click detectors…

Change to the loop that @MultipleStuds suggested as it ensures the box has the “Box” object and won’t result in the nil index.

1 Like
if otherBoxes[i].Box:FindFirstChild("DeleteBox") then
   otherBoxes[i].Box.DeleteBox.MaxActivationDistance = 0
end
1 Like

Tried this, but it still says that attempt to index nil with ‘DeleteBox’

Why don’t you use GetDescendants()

and just

local children = game:GetService("Workspace").CurrentMission:GetDescendants()

for i, v in pairs(children) do
    if v:IsA("ClickDetector") then
        v.MaxActivationDistance = 0
    end
end
1 Like

I would need to change a little thing in my previous script, but it seems to be the best solution. Thank you and @scripted_pj for the help :wink:

1 Like