How to return a table with a remote function?

Hi.
I’m trying to get player’s data via remote function to gui.
Basically, how would I send a table and then get it in local script as for i,v loop. By that, I mean.

This is what I have now:
In Local script: local datas = rFunc:InvokeServer()
and then, it returns from Server Script: return v.Name,cashData:Get(),timeData:Get()

so, in the same local script, I have a for in pairs loop/for i,v loop. Basically, I don’t know, how would i get that data.
Tried that, but didn’t work:

for i,v in pairs(datas) do
			local subClone = script.Subject:Clone()
			subClone.Parent = script.Parent.ScrollingFrame
			subClone.CashVal.Text = v[2]
			subClone.TimeVal.Text = v[3]
			subClone.Username.Text = v[1]
	end

And yeah, not working sadly.
(I didn’t put whole script, but problematic parts only.)

Thank you!

1 Like

Iterate through the table on the server and add to an array to a main array of which you’ll return.
For example:

local MainData = {}

for _, v in ipairs(yourData)
    table.insert(MainData, {v.Name, cashData:Get(), timeData:Get()})
end

return MainData

What should “yourData” be because it’s not defined in server script.

Whatever you were iterating through on the server, “v” lead me to believe you were looping through a table.

Okay, it returns now, but only for one player, but there are 2 players in my server rn.

Any errors in the output, if not could you possibly try debugging via the debugger or prints where neccessary?

No. That’s probably because of the in pairs loop in local script, but I don’t know how to fix. Basically, it works only for one player.

Oh wait. Doesn’t return halt whole script? So (server script), for i,v loop doesn’t fire next time…

Because, loop only prints once, not 2 times (because, there are 2 players in the game)

return exits a function and returns whatever follows.
It does not halt the full script, no.

Would you be able to show the function, then I’d get a better understanding.

Here you go:

rFunc.OnServerInvoke = function(player,name,beta,cash,skips,timePlayed)
	if player:GetRankInGroup(5372074) >= 190 then
		for i,v in pairs(game.Players:GetPlayers()) do
			local cashData = DataStore2("Cash",v)
			local timeData = DataStore2("Time",v)
			local MainData = {}
			table.insert(MainData, {v.Name,cashData:Get(),timeData:Get()})
			print(cashData:Get())
			return MainData
		end
	end
end

Ah, return here is exiting on the first player it gets to.
It’d be better here to use a RemoteEvent and FireClient each player on each iteration.

Then OnClientEvent in the LocalScript, apply whatever was sent over.

Okay, did that. But now, it still shows only one user as gui.
It prints 2 players in that for i loop, but shows only one gui…

Code:

for i,v in pairs(script.Parent.ScrollingFrame:GetChildren()) do
		if v:IsA("ImageLabel") then
			v:Destroy()
		end
	end
for i,v in pairs(dataTable) do
		--if datas[1] ~= script.Parent.ScrollingFrame:FindFirstChild(datas[1]) then
			print(v[1])
			local subClone = script.Subject:Clone()
			subClone.Parent = script.Parent.ScrollingFrame
			subClone.CashVal.Text = v[2]
			subClone.TimeVal.Text = v[3]
			subClone.Username.Text = v[1]
		--end
	end

If I don’t put that destroy event, it works. But I want old guis to be destroyed, how would i do that?

Okay, fixed.
Now, (not important) One question.
What was wrong with my previous table function so it didn’t work?

1 Like

Pretty much:
You invoke the server => It starts iterating => Returns back to you on the first iteration exiting the function.

1 Like