String to instance

hello all, Im trying to make this gui helper for myself and I’m trying to use attributes to locate a menu in a table just to make it easier, but I can’t seem to find it. is there an easier way I could go about make it super easy for myself to make a UI that opens up menus

for i,menu in pairs(menus:GetChildren()) do
	local menu = tostring(menu)
	table.insert(menuList,menu)
	print("inserted into table")
end

local function directButton()
	print("connected")
	print(direction)
	local tableloc = table.find(menuList,direction)
	print(tableloc)
	menuList[tableloc].Visible = true
end
1 Like

Full script? Where is direction defined? Why are you making a new variable with the same name as the menu in the for loop which is already defined? Why not just do menu.Name instead of tostring(menu)?

I was just trying new things out because I kept getting the error attempt to index nil. direction is defined as a string, which is inside the buttons gui. I’m just trying to make a simple system for myself to use in other games to make a ui.

So… do we get the full script for context?

local Players = game:GetService("Players")

local player = Players.LocalPlayer

local playerUI = script.Parent

local menuUI = playerUI:WaitForChild("menuTest")
local menus = playerUI:WaitForChild("menus")

local direction

local menuList = {}

for i,menu in pairs(menus:GetChildren()) do
	local menu = tostring(menu)
	table.insert(menuList,menu)
	print("inserted into table")
end

local function directButton()
	print("connected")
	print(direction)
	local tableloc = table.find(menuList,direction)
	print(tableloc)
	menuList[tableloc].Visible = true
end

local function checkForButtons(checkUI)
	for i,uiObject in pairs(checkUI:GetChildren()) do
		if uiObject:IsA("ImageButton") or uiObject:IsA("TextButton") then
			direction = uiObject:GetAttribute("menuDirect")
			uiObject.MouseButton1Click:Connect(directButton)
		else
			checkForButtons(uiObject)
			local test
		end
	end
end

checkForButtons(menuUI)

You insert a menu

but attempt to find an attribute?

Wait lemme re-read.

menuList references a screengui filled with all the menus

try

for i,menu in pairs(menus:GetChildren()) do
    table.insert(menuList,menu.Name)
	print("inserted into table")
end

In fact, try this script.

local Players = game:GetService("Players")

local player = Players.LocalPlayer

local playerUI = script.Parent

local menuUI = playerUI:WaitForChild("menuTest")
local menus = playerUI:WaitForChild("menus")

local menuList = {}

for i,menu in pairs(menus:GetChildren()) do
	table.insert(menuList,menu.Name)
	print("inserted into table")
end

local function directButton(direction)
	print("connected")
	print(direction)
	local tableloc = table.find(menuList,direction)
	print(tableloc)
	menuList[tableloc].Visible = true
end

local function checkForButtons(checkUI)
	for i,uiObject in pairs(checkUI:GetChildren()) do
		if uiObject:IsA("ImageButton") or uiObject:IsA("TextButton") then
			uiObject.MouseButton1Click:Connect(function()
                directButton(uiObject:GetAttribute("menuDirect")
            end)
		else
			checkForButtons(uiObject)
			local test
		end
	end
end

checkForButtons(menuUI)

on second thought, I think ill get rid of the attributes and try something else. I have a feeling this is a lot of unnecessary work

unfortunately this still gets the attempt to index nil error
thank you very much for your help though, much appreciated

I would like to continue to attempt to crack the problem, if you wouldn’t mind. Please reply if you are able to help me test out the code.

yeah ill be here. give er a whirl

Where exactly does the attempt to index nil error originate? Which line?

line 22, or

menuList[tableloc].Visible = true

attempt to index nil with visible

Why not store the instances and roll out your own findMenu() function.

for i,menu in pairs(menus:GetChildren()) do
	table.insert(menuList,menu)
end

local function directButton()
	local menu = findMenu(direction)
	menu.Visible = true
end

Add a print(tableloc) somewhere before the error, I have a feeling it prints the index.

no it prints nil for tableloc unfortunately. if it printed the index we could work it out i believe

try this

Okay, add a print(menuList) somewhere, and send a screenshot of your output.