Referencing Tables from Functions

Hello,

I want to be able to tell the script to see when you click on a Name in a GUI, you load that corresponding map. I have a function that helps me get the names of said maps, and it returns a table, but when I try to access that table later, there is a red underline on the [i] and the output won’t tell me what is wrong. I’ve tried redefining, I’ve checked for syntax errors (to the best of my knowledge) but I can’t seem to see the issue. Anyone know what’s wrong, or how to reference the table correctly?

Here is the code

local ReplicatedStorage = game.ReplicatedStorage
local Load = ReplicatedStorage.Load
local CurrentMap = game.Workspace:WaitForChild("CurrentMap")

local function GetNames()
	local Names = ReplicatedStorage:GetChildren()
	local NameList = {}
	for i,v in pairs(Names)do
		table.insert(NameList, v.Name)
	end
	return NameList
end

Load.OnServerEvent:Connect(function(player, vName)
	if vName == GetNames()[i]then
		print ("It Works")
	end
end)
1 Like

Try

local ReplicatedStorage = game.ReplicatedStorage
local Load = ReplicatedStorage.Load
local CurrentMap = game.Workspace:WaitForChild("CurrentMap")

local function GetNames(name)
	local NameList = {}
	for i,v in pairs(ReplicatedStorage:GetChildren())do
		table.insert(NameList, v.Name)
	end
if tostring(name) == tostring(NameList[1]) then
print("it works") -- make sure they have the same name
end
	return NameList
end

Load.OnServerEvent:Connect(function(player, vName)
	GetNames(vName)
end)