'Invalid table key type used' when using RemoteEvent

Howdy,

Just to preface this post, I have read this reply on the linked thread and it doesn’t exactly solve my problem.

I’m making a multiplayer-ish game (similar to MadLibs) that uses MessagingService to allow players to communicate responses, host lobbies, etc. across single-player servers. Below is a snippet of an UpdateTopics() function that throws a Invalid table key type used error whenever I fire the second argument on the last line.

Some notes:
The typeof() of the key is string. It always returns this. I have already tried converting it again using tostring() with no effect.
The format of the key itself is ‘UserId-(5-character room key)’ – for example: 24103210-JMGXB.

I added some comments to hopefully add some comprehension:

	-- update active games
	local filtered_games = {} --hide any hidden games// registry of all public rooms
	local n1 = 0 --public room count
	local n2 = 0 --private room count
	for k,RoomData in pairs(TOPICS.ACTIVE_GAMES) do --preliminary filter through all active games; 'RoomData' is a table
		if RoomData.PUBLIC == true then --check for boolean 'PUBLIC'
			filtered_games[k] = RoomData --add public room to registry
			print(typeof(k)) --always returns 'string'
			n1 = n1+1 --add to count
		end
		n2 = n2+1 --add to count
	end
	--print(n2,'total rooms/',n1,'public rooms/',n2-n1,'private rooms/')
	
	REMOTES.CLIENT_UPDATE:FireAllClients(TOPICS.ONLINE_PLAYERS,filtered_games) --when firing 'filtered_games', it throws the error
1 Like

Hello, you said it errors on the last line of this snippet? If so, you are going to have to provide more information about how your REMOTES table is configured.

Also, it wouldn’t hurt to see how RoomData and your TOPICS table is configured also.

REMOTES is a folder in ReplicatedStorage that holds all of my RemoteEvents and RemoteFunctions.

local TOPICS = {
	['ONLINE_PLAYERS'] = {};
	['CURRENT_GAME'] = {};
	
	['ACTIVE_GAMES'] = {};
}

local RoomData = {
	['SIGNAL'] = 'createroom';
	['ROOMKEY'] = fullkey;
	['HOST'] = UserId;
	['PLAYERS'] = {};
	['PACKS'] = filteredpacks;
	['PUBLIC'] = ispublic;
	['target'] = 'ACTIVE_GAMES';
	['OPEN'] = true
}

I’ll PM you any more code if needed.

Could you try making it so that your key format has a letter or word in front instead of starting with numbers? I’m not sure if this will do anything but I don’t see what else would be the problem

Also, maybe print and share a JSONEncoded version of filtered_games so it is easier to see your problem?

Output of RoomData
filtered_games itself is not encoded, as the server instantly decodes a message once it gets it.
I also tried switching the order of the Room Key and host ID’s but the error still persists.

If anything, I’ll just rebuild the system from the ground-up. It’s not too lengthy of a script anyways.

What I mean is print filtered_data using HttpSercice:JSONEncode() so that I can take a look at the entire table, as I am still having a little bit of trouble picturing the entire system

I think I see what the problem is, it isn’t RoomData, it’s in filtered_games.

When you’re adding RoomData to filtered_games, you’re using the index from TOPICS.ACTIVE_GAMES, which doesn’t account for data that fails to pass the RoomData.PUBLIC == true condition. If RoomData.PUBLIC at ACTIVE_GAMES[1] just so happens to be false, the first value in filtered_games would be 2, or 3, or whatever the first public room is. Here’s what it could like:

-- filtered_games:
{
    [2] = <RoomData>
    [3] = <RoomData>
    [5] = <RoomData>
}

Instead, you should use table.insert() to add data to filtered_games, assuring the desired behavior.