Issue with Button Click: 'Button or object not found' error

Hello! I’m having an issue with my code. When I click a button, it displays the error message “Button or object not found.” What could be causing this problem, and how can I fix it? I’ve double-checked the paths and ensured that I’m waiting for elements to appear. What might be wrong?

image

image

localscript (ObjectVisibilityScript):

-- Assuming your buttons are directly inside 'buttonList_Table1'
local buttonsFolder = script.Parent.Parent.buttonList_Table1

local buttonToObjectMap = {
	button1 = buttonsFolder:WaitForChild("button1"),
	button2 = buttonsFolder:WaitForChild("button2"),
	button3 = buttonsFolder:WaitForChild("button3"),
	button4 = buttonsFolder:WaitForChild("button4"),
	button5 = buttonsFolder:WaitForChild("button5"),
	button6 = buttonsFolder:WaitForChild("button6"),
	-- Add the rest of the buttons and objects here
}

local objectsLists = {
	Object1 = game.Workspace.Elements.Deliverables.table_1.Objects.Object1,
	Object2 = game.Workspace.Elements.Deliverables.table_1.Objects.Object2,
	Object3 = game.Workspace.Elements.Deliverables.table_1.Objects.Object3,
	Object4 = game.Workspace.Elements.Deliverables.table_1.Objects.Object4,
	Object5 = game.Workspace.Elements.Deliverables.table_1.Objects.Object5,
	Object6 = game.Workspace.Elements.Deliverables.table_1.Objects.Object6,
	-- Add the rest of the buttons and objects here
}

-- Function to handle button click
local function handleButtonClick(button, object)
	if button and object then
		button.MouseButton1Click:Connect(function()
			print("Button clicked: " .. button.Name)
			print("Changing visibility of associated object: " .. object.Name)

			-- Change the visibility of the associated object
			object.Transparency = 0 -- You can adjust this to 0 if needed to make it fully visible

			print("Visibility changed.")
		end)
	else
		print("Button or object not found.")
	end
end

-- Connect the button click for each button and object
for buttonName, objectName in pairs(buttonToObjectMap) do
	local button = buttonsFolder[buttonName] -- Use direct reference within the folder
	local object = objectsLists[objectName]

	-- Call the function to handle button click
	handleButtonClick(button, object)
end
1 Like

You can use pairs loop instead of tables. This means you could loop through the parent of the objects you’re trying to access and get all of them at once.

You can also use the collectionservice to tag every object you want to use and run through all of them in a pairs loop. That’s if they are parts.

This is just to make sure things are much quicker and no specific issues like you are having happen.

This won’t work. objectName is an instance of button. So when you then do object = objectsList[objectName], what you’re actually doing is objectsList[buttonObject]. If you look at objectsLists, all the keys are strings. Not button objects.

@ExercitusMortem @Violet_sheer
I’ve used both ideas that you gave me, and it’s still not working. How could I solve this? I would like to do it this way because it’s more optimal and not as much code.

I mean, I didn’t give you any idea. I just told you why it didn’t work, it was up to you to find the solution.

local objectsLists = {
	button1 = game.Workspace.Elements.Deliverables.table_1.Objects.Object1,
	button2 = game.Workspace.Elements.Deliverables.table_1.Objects.Object2,
	-- Add the rest of the buttons and objects here
}

for buttonName, objectName in pairs(buttonToObjectMap) do
	local button = buttonsFolder[buttonName] -- Use direct reference within the folder
	local object = objectsLists[buttonName]

I recommend using an array to list out the objects/buttons instead of a dictionary.

local buttonToObjectMap = {
	buttonsFolder:WaitForChild("ExampleButton");
}

local objectsLists = {
	game.Workspace.Elements.Deliverables.table_1.Objects.ExampleObject;
}
for i, button in ipairs(buttonToObjectMap) do
	local object = objectsLists[i]
	handleButtonClick(button, object)
end

My solution uses the index of objects in arrays and matches them accordingly. You can do this using a dictionary but I think this solution is a bit simpler.

I hope this helps!

1 Like

Oh, thank you so much! It worked, I made the changes and adapted my code based on your suggestions, and it worked. Thank you very much! and Thanks to everyone equally for the help.

code updated version:

-- Assuming your buttons are directly inside 'buttonList_Table1'
local buttonsFolder = script.Parent.Parent.buttonList_Table1

-- Use arrays instead of dictionaries
local buttonsArray = {
	buttonsFolder:WaitForChild("button1"),
	buttonsFolder:WaitForChild("button2"),
	buttonsFolder:WaitForChild("button3"),
	buttonsFolder:WaitForChild("button4"),
	buttonsFolder:WaitForChild("button5"),
	buttonsFolder:WaitForChild("button6"),
	-- Add the rest of the buttons here
}

local objectsArray = {
	game.Workspace.Elements.Deliverables.table_1.Objects.Object1,
	game.Workspace.Elements.Deliverables.table_1.Objects.Object2,
	game.Workspace.Elements.Deliverables.table_1.Objects.Object3,
	game.Workspace.Elements.Deliverables.table_1.Objects.Object4,
	game.Workspace.Elements.Deliverables.table_1.Objects.Object5,
	game.Workspace.Elements.Deliverables.table_1.Objects.Object6,
	-- Add the rest of the objects here
}

-- Function to handle button click
local function handleButtonClick(button, object)
	if button and object then
		button.MouseButton1Click:Connect(function()
			print("Button clicked: " .. button.Name)
			print("Changing visibility of associated object: " .. object.Name)

			-- Change the visibility of the associated object
			object.Transparency = 0 -- You can adjust this to 0 if needed to make it fully visible

			print("Visibility changed.")
		end)
	else
		print("Button or object not found.")
	end
end

-- Connect the button click for each button and object
for i, button in ipairs(buttonsArray) do
	local object = objectsArray[i]

	-- Call the function to handle button click
	handleButtonClick(button, object)
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.