How would I send a dictionary/array using RemoteEvents?

Hello there. I will try to keep this topic as short as possible. I was making a custom player board, but I came across an error. When I tried sending an array and a dictionary, I got an Cannot convert mixed or non-array tables: keys must be strings error. I am guessing that the error is caused by the dictionary rather than the array, but I am not really sure. Here is the code that fires the event:

local Module = {}

-- Variables --

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("RefreshPlayerListEvent")

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

local TeamList = {}
local PlayerList = {}

-- Scripting --

function Module:UpdatePlayerList(Player1, Player2)
	for i, v in pairs(Teams:GetChildren()) do
		if v.Name ~= "Choosing" and #v:GetPlayers() > 0 then
			TeamList[v:WaitForChild("LayoutOrder").Value] = v.Name
			local TemporaryPlayerList = {}
			for o, p in pairs(v:GetPlayers()) do
				table.insert(TemporaryPlayerList, p.Name)
				table.sort(TemporaryPlayerList, function(a, b) return a:lower() < b:lower() end)
			end
			PlayerList[v.Name] = TemporaryPlayerList
		end
	end
	if Player1 then
		Event:FireClient(Player1, TeamList, PlayerList)
		if Player2 then
			Event:FireClient(Player2, TeamList, PlayerList)
		end
	elseif not Player1 or not Player2 then
		Event:FireAllClients(TeamList, PlayerList)
	end
end

return Module

If you could help me with my problem, that would be awesome. If sending a dictionary is impossible, then how could I achieve the same goal using other ways. Thank you.

2 Likes

Basically: it means that you can’t send a mixed table (a table with both integer and string indexes), like this:

local re = game.ReplicatedStorage.RemoteEvent
local mixedTbl = {Jimmy = "John",[17]=5} --an example of a mixed table
re:FireAllClients(mixedTbl)-- Cannot convert mixed or non-array tables: keys must be strings

However, both arrays and dictionaries can be sent via a remote event

local re = game.ReplicatedStorage.RemoteEvent

local array = {1,5}

local dict = {Jeff = 1,Jim = 5}

re:FireAllClients(array,dict)--assuming `game.ReplicatedStorage.RemoteEvent` exists, there would be no errors
4 Likes

Thank you for your reply. Now I understand why it errors. I will try converting all players to one string and separating them by a comma. I have to go now, so I will try it later. Again, thank you for your reply.

An easier way would be to JSON:Encode it from HTTP service, since if I’m not mistaken that JSON Stringifies it.

5 Likes

Yep, JSON Encode outputs a string so that should work. Also, it’s compatible with dictionaries.

3 Likes

That worked just how I wanted it to. Thank you so much!

1 Like