Why is the result of sending a dictionary through a RemotEvent always "nil"?

I’m trying to send an inventory from a datastore in a script to where it is used, in a LocalScript. I’m at a loss as to why I cant send this dictionary from the server to the client without it appearing as nil.

--LocalScript
  rs = game:GetService("ReplicatedStorage")
remote = rs:WaitForChild("SendData")

data = {
	ownedcolours = {"White"},
	ownedfaces = {"Raig"},
	ownedgloves = {"Red"},
	ownedhats = {}
}

local function datafound(player, data)
	data = data
	print(data)
end

remote.OnClientEvent:connect(datafound)

–Script in ServerScriptService
local ds = game:GetService(“DataStoreService”)
local datastore = ds:GetDataStore(“InventorySaveSystem”)
local rs = game:GetService(“ReplicatedStorage”)
local remote = rs:WaitForChild(“SendData”)
game.Players.PlayerAdded:connect(function(player)
local sentdata = datastore:GetAsync(player.UserId) or {
ownedcolours = {“White”},
ownedfaces = {“Raig”},
ownedgloves = {“Red”},
ownedhats = {}
}
datastore:SetAsync(player.UserId, sentdata)
remote:FireClient(player, sentdata)
end)

any help would be great, sorry if the formatting is terrible and/or my code is hard to read, I’ve never tried to ask for help before

1 Like

OnClientEvent handlers don’t receive an argument identifying the receiving player, because it only handles events for the client’s local player.

So your data goes into the first argument to datafound, which is player. You need to delete that argument.

Also you’ve written

	data = data

It looks like you’re trying to set the global variable data to the value of the argument data, but this just sets the argument data to itself.

3 Likes

your implementation is a bit incorrect but doing this should correct your problem:

--in your local script change your function to this one:

local function datafound(ServerData)
	data = ServerData
	print(data)
end

when using remote events from the server to client, on the client you need not have a player argument, only what comes after.

2 Likes