Attempt to index nil with 'MouseClick'

Hello, everyone!

I’m working on a door system script. But, when I run the script, I get this error:

Workspace.Bunker.DoorSystem.Script:53: attempt to index nil with 'MouseClick'

Anything wrong? Here’s my code:

-- Service
local service = game:GetService("TweenService")

-- Shortcuts
local door1 = script.Parent.Door1
local door2 = script.Parent.Door2
local buttons = {}
local time = 3

-- Animations
local open1 = service:Create(door1, TweenInfo.new(5, Enum.EasingStyle.Linear), {Position = Vector3.new(-33, 9, 104.3)})
local close1 = service:Create(door1, TweenInfo.new(5, Enum.EasingStyle.Linear), {Position = Vector3.new(-33, 9, 112.9)})
local open2 = service:Create(door2, TweenInfo.new(5, Enum.EasingStyle.Linear), {Position = Vector3.new(-33, 9, 131.65)})
local close2 = service:Create(door2, TweenInfo.new(5, Enum.EasingStyle.Linear), {Position = Vector3.new(-33, 9, 122.9)})

-- Preperations
for i, v in pairs(script.Parent:GetChildren()) do
if v.Name == "Button" then
table.insert(buttons, "Button")
end
end

-- Functions
function changeColor(color)
for i, v in pairs(buttons) do
v.Color = color
v.PointLight.Color = color
end
end

function changeDistance(distance)
for i, v in pairs(buttons) do
v.ClickDetector.MaxActivationDistance = distance
end
end

-- Main Function

function onClicked(playerWhoClicked)
open1:Play()
open2:Play()
changeColor(Color3.new(1, 0, 0))
changeDistance(0)
wait(time)
close1:Play()
close2:Play()
changeColor(Color3.new(0, 1, 0))
changeDistance(32)
end

for i, v in pairs(buttons) do
v.ClickDetector.MouseClick:Connect(onClicked)
end

You are inserting a string into the table instead of the object:

table.insert(buttons, "Button")

Do this:

table.insert(buttons, v)
2 Likes

When you insert a button into your buttons table, you aren’t actually inserting a button. (this part is located in your ‘Perperations’ section)

Your current code says
table.insert(buttons, “Button”); – this inserts a string into your buttons table

but what you should be doing is
table.insert(buttons, v); – this inserts the button

1 Like

Both of your solutions worked, thanks!
(I wonder why using a string errors instead of arrays.)

What do you mean by that?

Please explain more

I was unaware that using a string errors because I’m not inserting the object itself.