Can't return a table through a RemoteFunction

I have an issue, it’s that for some reason my client sided script reaches the return, but doesnt end up returning it.

Client Side:


RS["Get Training Players"].OnClientInvoke = function()

	local TrainingPlayersTable = {}

	for Number, Template in pairs(script.Parent.Main["Host Window"]["Player List"].List:GetChildren()) do
		if Template.Name ~= "ListLayout" then
			table.insert(TrainingPlayersTable, Number, {PlayerID = Template["Player ID"].Value, PlayerName = Template.Trainee.Text, PlayerRank = Template.Rank.Text, MinistryRank = RS.MinistryRank.Value, MainGroupRankNumber = Template["Main Group Rank Number"].Value, IsCoHost = Template.IsCoHost.Value})
		end
	end

	print(TrainingPlayersTable)

	return TrainingPlayersTable

end

It prints the table, but the next RemoteFunction after it doesnt print at all.

Server Side:

		print(RS["Get Training Players"]:InvokeClient(Player))
		print(RS["Get Training Music"]:InvokeClient(Player))

The print(RS["Get Training Players"]:InvokeClient(Player)) prints on the client,

but print(RS["Get Training Music"]:InvokeClient(Player)) doesn’t so,

it has to stop at the “Get Training Players” remote.

3 Likes

Is it because you have the right OnClientInvoke for "Get Training Players" but you don’t have a OnClientInvoke function for "Get Training Music"?

It’s becuase you’re returning an object not a simple array. You can’t send objects through remote functions.

Is there a way I can return it then?

Yeah, you have to return an array.

local a = {Name = "test", something = 5} -- it's an object
local b = {"test", 5} -- it's an array

You have to return something like b, becuase it’s an array and a is an object [learn more: object oriented programming]. So I suggest converting the object to a simple array and then pass it through.

1 Like

Alright, thank you! :slightly_smiling_face:

1 Like

I found the source of information for you about what can be passed with remote events and remote functions: Custom Events and Callbacks | Documentation - Roblox Creator Hub - read under ‘Parameters’.

So, I thought I would clarify your response. Both a and b in your example can be passed through a RemoteEvent, RemoteFunction, BindableEvent, or BindaleFunction. However, a table with keys and an array cannot be used.

{ Name = "Test", Value = 5 } ✔️
{ 1, 2, 3 } ✔️

{ 1, 2, 3, A = "A", B = "B" } ❌
4 Likes

I thought I only used the

scenario?

Not necessarily,

You could send two tables then combine them later or send two sub tables,

You can send Instances through remotes as long as they are replicated,

The problem with Remote and Bindable Instances are they can only serialize data as much as JSONEncode can, only string keys, no mixed-tables, cyclic tables and no Metatables.

I dont see where I mix keys and arrays though.

You’re sending an array with ‘objects’ inside: {{Name = “test”}, {Name = “test2”}}.

So, what would be a good solution?

I don’t think I can send all the subtables separately.

Those aren’t Instances, they are strings, to make an Instance as a key you’ll need to enclose them in []


@tankattack9 you aren’t using a Mix-Table, I was elaborating on your question

Can I ask why you are using on Invoke Client, it’s not recommend

Have you heard about object oriented programming?

That’s not the issue of the OP’s problem, I send dictionaries across the network all the time.

I’m using InvokeClient mainly because it’s the first thing that came to my head about returning data from client, as that’s what it’s made for.

I had a similiar problem and this was my solution:

{{a = "a", b = "b"}, {a = "x", b = "y"}}

{"a", "b", "x", "y"}

and then you know that two first elements are from the first element of the original array and so on.

2 Likes

But I didn’t say that these are instances so I don’t know what you are talking about.

I assumed you miss understood that the OP is using Instances as a key, as you mentioned Objects, I don’t consider Dictionaries as objects but could just be me.