*cannot read value as a string

I’m making a module script that returns a function that sets connections to a specific object for more efficiency. I want to return a table with the key being the connection’s name and the value being the connection itself. However, when I print this table before returning it, it prints:

["MouseLeave"] = *cannot read value as a string

The script:

local connections = {} 

return function(tabela, instancia)
	for conexao, parametros in tabela do
		if typeof(conexao) ~= "string" or typeof(parametros) ~= "table" then continue end

		local connection = instancia[conexao]:Connect(function(...)
			local f = parametros[1]

			if parametros[#parametros] then
				f(table.unpack(parametros, 2, #parametros - 1), ...)
			else
				f(table.unpack(parametros, 2))
			end
		end)

		connections[conexao] = connection
	end

    print(connections)

    return connections
 end

I want to know why it prints like that, and why it can’t read the Connection. Because it cannot, the connection is not returned correctly.

Functions cannot be converted into a string (same as events), presumably this is something to do with it’s compilation process and how it is stored.

The logic here asks if there is a value at the last index, so the else would never be reached unless the table was empty but you’ve already set f to be the value of the first index in the table.
The unpacking also would miss the last index of parametros.

Perhaps you meant something like this:

local connection = instancia[conexao]:Connect(function(...)
			local f = parametros[1]

			if #parametros > 2 then
				f(table.unpack(parametros, 2, #parametros ), ...)
			else
				f(table.unpack(parametros, 2))
			end
		end)

Yes! I had fixed it before, thanks for remembering. So, is this problem an error or just because you can’t print functions? That’s all I need to know.

Additionally, I’ve found another error: if the variadic argument is nil, the f function would pass as arguments out of place. For instance, let’s suppose parametros is {“0”, “1”, “2”, “3”, true}. table.unpack(parametros, 2, #parametros - 1) would return “1”, “2”, “3”; however, when I pass it to the function along with the variadic argument, which is nil in this case, it would return only the first argument, “1”. So I had to create an IF statement checking if … is nil

You cannot print functions. (As far as I know, there may be some hacky way around it but I’m not sure)

2 Likes

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