Remote function not sending dictionary - Invalid table key type used

I am trying to send a dictionary to a Localscript using a remote function but i’m getting an error “Invalid table key type used”

Script

local RemoteFunction = game.ReplicatedStorage.RemoteFunction

local leaderBoard = {
	[1739189] = {
		Kills = 0,
		Assists = 0,
		Deaths = 0,
		Cash = 0
	}
}


function sendStats(player)
	return leaderBoard
end

RemoteFunction.OnServerInvoke = sendStats

LocalScript

-- This script is inside a image button

script.Parent.Activated:Connect(function()
	local repStorage = game:GetService("ReplicatedStorage")
	local RemoteFunction = repStorage:WaitForChild("RemoteFunction")
	local stats = RemoteFunction:InvokeServer() --Error is occuring here
	
	local example = stats[game.Players.LocalPlayer.UserId]["Kills"]
	print(example)
end)

According to Custom Events and Callbacks | Documentation - Roblox Creator Hub " sub-tables do not need to be indexed in the same way as their parent"

Additional information
I’m storing leaderboard stats in a global script and want to send them to the client when they click an image button to bring up a custom GUI leaderboard.

local example = stats[game.Players.LocalPlayers]["Kills"] seems to be the issue. The table seems to be storing what I assume is a UserID, so shouldn’t it be LocalPlayer.UserId (I am assuming that you append stats to the table for each user that is in the server).

2 Likes

The error is occuring on the line local stats = RemoteFunction:InvokeServer(), but you are right that it should have been LocalPlayer.UserId I’ll fix that now.

Hmm, then could it perhaps be the fact that the Key for the tables within the dictionary is an integer? Perhaps make it a string using tostring(Player.UserId) (that is assuming that values are added to the table when players join), and then in the local script after you receive the table through InvokServer (assuming this works), you could call values with stats[tostring(game.Players.LocalPlayers.UserId)]["Kills"]

2 Likes

When You Invoke The Server You Can Just Invoke the client too in the server pass the arguments of the table then put it as a parameter in the client

because

A RemoteFunction is designed for two-way communication , such that it can send information across the server-client boundary and then wait for a response from the other side.