'Unknown Symbol' and 'Duplicate Definition' when using dictionaries and functions

Hello!

I am trying to assign a function to a lobby, however I keep getting ‘Unknown Symbol’ when trying to check for a certain lobby or assign a specific function to a lobby.

So basically, I have a dictionary handling all of the lobby info of each lobby, however when assigning a countdown function, I am left with ‘Unknown symbol’ from the dictionary, and ‘Duplicate definition’ from the function, how do I fix it?

For example,

local Lobbies = {
	['Lobby1'] = {
		['HasStarted'] = false,
		['Function'] = Lobby1(), -- underlined with 'unknown symbol'
		['MaxPlayers'] = 2,
		['Players'] = {}
	},
	['Lobby2'] = {
		['HasStarted'] = false,
		['Function'] = Lobby2(),
		['MaxPlayers'] = 3,
		['Players'] = {}
	},
	['Lobby3'] = {
		['HasStarted'] = false,
		['Function'] = Lobby3(),
		['MaxPlayers'] = 5,
		['Players'] = {}
	},
	['Lobby4'] = {
		['HasStarted'] = false,
		['Function'] = Lobby4(),
		['MaxPlayers'] = 8,
		['Players'] = {}
	}
}

function Lobby1--[[underlined with 'duplicate definition]]()
	for i = 30, 0, -1 do
		for i2,v2 in pairs(Lobbies['Lobby1']['Players']) do
			
		end
	end
end

try moving this to before the tables.

1 Like

Tried it before posting it, there is still an error.

Can you tell me what the error was?

1 Like
	for i2,v2 in pairs(Lobbies--[[Unknown symbol 'Lobbies']]['Lobby1']['Players']) do
		
	end

try making a whole different table for functions, you can have the same layout. Have the lobby functions before that table though.

1 Like

So like

Lobby functions table
Actual functions
Lobby info?

no, like this
lobby info
actual functions
functions table

1 Like

Sounds like an editor bug. Try restarting studio.

1 Like

its not that, hes trying to call a function that doesnt exist yet.

1 Like

Ah, I understand now. My bad-!

1 Like

Still doesn’t work, it seems that your solution overwrites the table. I will show you my script:

local Lobbies = {
	['Lobby1'] = {
		['HasStarted'] = false,
		['MaxPlayers'] = 2,
		['Players'] = {}
	},
	['Lobby2'] = {
		['HasStarted'] = false,
		['MaxPlayers'] = 3,
		['Players'] = {}
	},
	['Lobby3'] = {
		['HasStarted'] = false,
		['MaxPlayers'] = 5,
		['Players'] = {}
	},
	['Lobby4'] = {
		['HasStarted'] = false,
		['MaxPlayers'] = 8,
		['Players'] = {}
	}
}

function Lobby1()
	for i = 30, 0, -1 do
		for i2,v2 in pairs(Lobbies['Lobby1']['Players']) do
			
		end
	end
end
function Lobby2()
	
end
function Lobby3()
	
end
function Lobby4()
	
end

local Lobbies = {
	['Lobby1'] = {
		['Function'] = Lobby1()
	},
	['Lobby2'] = {
		['Function'] = Lobby2()
	},
	['Lobby3'] = {
		['Function'] = Lobby3()
	},
	['Lobby4'] = {
		['Function'] = Lobby4()
	},
}

Later in the script,

		table.insert(Lobbies['Lobby'..lobby]['Players'], player)

Error:
ServerScriptService.LobbyHandler:92: invalid argument #1 to 'insert' (table expected, got nil)

You could have it like

['Function'] = function(self)
    print('Calling function!')
    print('Max Players: ' .. self.MaxPlayers)
end

You call it like:

-- self is automatically passed when using ':' instead of '.'
Lobbies['Lobby1']:Function()

You could look more into the self parameter in Lua if you don’t know about it.
If you wanted these functions to be called immediately then you could just call those under the table, as it seems you’re trying to call Lobby1() in the table.

1 Like

Nah, I just want it there as a variable to be conveniently called whenever, instead of doing a huge if statement.

I don’t think I can do it how you did it, can I? I am wrapping that function in a coroutine as the function is a timer, and multiple timers can’t run at once in a single script w/o coroutines.

Also, does self just refer to the same script, kind of like Me in VB?

check if there is such a lobby you are trying to add the player to

1 Like

print out the lobby number you are trying to find

1 Like

I dont have experience in VB, self is just a reference to the table the function is in. So self in our case is the Lobbies['Lobby1'].

If you’re looking for it to be a variable to be called at any time then yes, this is the solution you want. If you’re wrapping it in a coroutine, then just pass in the table since self is that table.
coroutine.wrap(Lobbies.Lobby1.Function)(Lobbies.Lobby1)

1 Like

For example:

local John = {
    Name = 'John',
    Age = 23,
    Hello = function(self)
        print('My name is ' .. self.Name .. ' and I am ' .. self.Age .. ' years old!')
    end
}

coroutine.wrap(John.Hello)(John); -- Prints out 'My name is John and I am 23 years old!'
-- This is equivalent to: John:Hello() or John.Hello(John)

First print: My username, Lobby number
Second print: print('Lobbies'..lobby)

Screen Shot 2020-08-30 at 9.25.53 PM

If Lobbies1[‘Players’] exists, as it should, the player should have been added to that table.

@ILoveMyJelly

Could I just do Lobbies1['Function']()?

Yes. Make sure to pass in self if you’re not using : though. self would still be Lobbies.Lobby1

1 Like