Number keys get turned into strings when passed through remote events?

I am working on a modulescript (dictionary specific) replicator for my game.

When I pass a table through a remote event, such as the one in this pseudo code:

local members = {
    58293471828 --[[random player's userid]] = "owner" --[[player's role]]
}

FireClients(members)

The number based userid key seems to be turned into a string when received by the client. Here is an example of this happening.

Server Side
image

Client Side
image

Does anybody know why this is happening? I am assuming that this is due to some sort of array/dictionary messup in the interpreter, but I want to be sure. If there is a way to fix this, or a way to take advantage of this issue without running tostring() on a player’s userid every time it is used, please let me know.

(Extra Note: Yes, I know I could cache player userids in some sort of table to retrieve their string form to bypass the constant “tostring” usage, but that would create dependencies in my modules that I do not want).

2 Likes

You can convert the dictionary to a pair of arrays and then convert that pair back to a dictionary on the other side of the network.

local function convertDictionaryToTwoArrays<TKey, TValue>(dictionary: {[TKey]: TValue}): ({TKey}, {TValue})
	local keyArray: {TKey} = {}
	local valueArray: {TValue} = {}
	for key: TKey, value: TValue in dictionary do
		table.insert(keyArray, key)
		table.insert(valueArray, value)
	end
	return keyArray, valueArray
end

local function convertTwoArraysToDictionary<TKey, TValue>(keyArray: {TKey}, valueArray: {TValue}): {[TKey]: TValue}
	if #valueArray ~= #keyArray then
		error("Array lengths don't match.")
	end
	local dictionary: {[TKey]: TValue} = {}
	for arrayIndex: number, dictionaryKey: TKey in keyArray do
		dictionary[dictionaryKey] = valueArray[arrayIndex]
	end
	return dictionary
end

This way, the data type of the keys of the dictionary can be any datatype that can be sent via a remote event. So this would work even for a dictionary whose keys are Instances.

2 Likes

Thanks both of you. I wish I could mark your posts as solutions if I could. Though I might have to mess around with the code you sent me a little, bit, I appreciate the help here.

@RoBoPoJu @frogweezer

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