Remote Function Returning nil

Hi!
I’m trying to have the item amount values in my inventory system be loaded from DataStores. To do this, I have set up a remote function named “GetItemsFunction”. However, the Remote Function is not working how I currently understand it to. Even when there are values in the data store the remote function returns nil:

Client:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteFunction = ReplicatedStorage:WaitForChild("GetItemsFunction")

local Data = remoteFunction:InvokeServer()
warn("Data:")
warn(Data)

Server:

local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GetItemsFunction = ReplicatedStorage:WaitForChild("GetItemsFunction")

local function onGetData(player, treasureId)
	print(player.Name .. " fired the remote event")
	local ItemStore = DataStoreService:GetDataStore("Player"..player.UserId .."ItemStore_0.0.1")
	
	print("[Server] Getting Data...")
	local success, currentItemAmount = pcall(function()
		return ItemStore:GetAsync("Item1Amount")
	end)
	if success then
		print("[Server] Got Data")
		warn(currentItemAmount)
		warn(ItemStore:GetAsync("Item1Amount"))
	end
end

GetItemsFunction.OnServerInvoke = onGetData

In the server I have the script warn the item amounts. Here are the messages:
image

Im not sure why this isn’t working, although this is my first real time working with remote functions (rather than remote events)
I have looked on the Dev Form, YouTube, and the main tutorials in the Roblox documentation.
Thanks in advanced! Hopefully I didn’t just mistype something lol.

1 Like

Remote data transmission is finicky about the kind of data you can send. Most often you can’t send any sort of instance. But you can send the name of it or the location of the instance in the game or the asset id or something. What value/values are you expecting?

Edit: Also I don’t believe you can return the value that is returned by the GetAsync() method. You’ll likely need to pluck out the specific values and send those in a table or individually. GetAsync() returns 2 values actually, the value you stored and a DataStoreKeyInfo instance.

You are printing the returned value of invoking the remote itself this will obviously return nil also try adding the argument treasureId, i think you forgot it add it where it says remoteFunction:InvokeServer(here)

It was actually a scope issue. You were only returning the value you got from the GetAsync() method from inside the pcall function. This simply put the value inside the “currentItemAmount” variable.

Outside of that pcall you’d be within the scope of the onGetData() function. In that case you weren’t actually returning anything at all.

Try:

local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GetItemsFunction = ReplicatedStorage:WaitForChild("GetItemsFunction")

local function onGetData(player, treasureId)
	print(player.Name .. " fired the remote event")
	local ItemStore = DataStoreService:GetDataStore("Player"..player.UserId .."ItemStore_0.0.1")
	
	print("[Server] Getting Data...")
	local success, currentItemAmount = pcall(function()
		return ItemStore:GetAsync("Item1Amount") --this returns from the pcall function
	end)
	if success then
		print("[Server] Got Data")
		warn(currentItemAmount)
		warn(ItemStore:GetAsync("Item1Amount"))

		return currentItemAmount --return the value to the client
	end
end

GetItemsFunction.OnServerInvoke = onGetData
2 Likes

Thanks so much! This works completely, I didn’t know previously you had to use return twice, although that makes sense. This will certainly help me in the future as well as now. Thanks again! Also srry for the late reply, I was away.
image

Also thanks for the reply @FuzzyEq .