Stuck With My Slot Saving System

So, i’m trying to see if I can create a slot saving system. The thing is, the RemoteFunction must not be firing.

SERVER

local slots = {'slot1', 'slot2', 'slot3', 'slot4'}
local DSS = game:GetService("DataStoreService")

game.ReplicatedStorage.Events.Functions.GetDataSlot.OnServerInvoke = function(slot)
	for _,v in pairs(slots) do
		if slot == v then
			local DataStore = DSS:GetDataStore(v)
			if DataStore ~= nil then
				print(string.format("Loaded Save Slot: %s", v))
			else
				print(string.format("Creating Slot: %s", v))
			end
			print("Nope:  "..v)
		end
	end
end

LOCAL

script.Parent.Slot1.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.Events.Functions.GetDataSlot:InvokeServer("slot1")
end)

No output, no nothing. Thanks if you can help!

Firing a RemoteFunction must return something. If your remote isn’t supposed to return anything back to the client, you should use a RemoteEvent instead.

1 Like

Might be worth you doing something like this rather than looping through a table:

local slots = {slot1 = true; slot2 = true; slot3 = true; slot4 = true}
local DSS	= game:GetService 'DataStoreService'

game.ReplicatedStorage.Events.Functions.GetDataSlot.OnServerInvoke = function (slot)
	if type(slot) == 'string' and slots[slot] then
		local DataStore = DSS:GetDataStore(slot)
		if DataStore then
			print(string.format("Loaded Save Slot: %s", slot))
		else
			print(string.format("Creating Slot: %s", slot))
			-- create the stuff
		end
		
		return some_data_here_from_DataStore
	end
	return nil
end

As @Dev_Kittenz said, you need to return the data (which I assume is the intended function since the remote is called ‘GetDataSlot’).

Also, in the client sided script, you’re not defining a variable from the result of the invocation:

script.Parent.Slot1.MouseButton1Click:Connect(function()
	local result = game.ReplicatedStorage.Events.Functions.GetDataSlot:InvokeServer("slot1")
	print('I got given:', result, 'from the server!')
end)

P.S. You can’t return a DataStore to the client if that’s what you’re going to do (since the slot seems to be a DataStore?).

I even added a return, nothing happens.

I tried it. Nothing happened sadly. Not sure why. It prints the stuff from the LocalScript but nothing from the ServerScript.

Can you send an .rbxl file with the architecture of the script(s) in question?

If not:

  1. Is the server code in ServerScriptService?
  2. Is your client code in a localscript?
  3. Is the ‘Slot2’ that you’re trying to hook a MouseButton1Click button the correct button?
  4. Are you sure you’re firing the same RemoteFunctions?
  5. Is there anything in output?
  6. Can you add more debug prints to output and show us the results

Thing.rbxl (23.5 KB)

I didn’t see it before, but it’s because you haven’t accounted for the fact that the 1st argument of OnServerInvoke when called is the player that fired the invocation. As such, you would need the following as your server code:

local slots = {slot1 = true; slot2 = true; slot3 = true; slot4 = true}
local DSS	= game:GetService 'DataStoreService'

game.ReplicatedStorage.Events.Functions.GetDataSlot.OnServerInvoke = function (player, slot)
	if type(slot) == 'string' and slots[slot] then
		local DataStore = DSS:GetDataStore(slot)
		if DataStore then
			print(string.format("Loaded Save Slot: %s", slot))
		else
			print(string.format("Creating Slot: %s", slot))
			-- create the stuff
		end
		
		return "Worked"
	end
	return nil
end

Please note though, that this obviously won’t work if the game isn’t published and/or you haven’t got access to datastores enabled (if not, you’ll see an error that reads something along the lines of ‘you must publish this place to the web to access DataStore’)

1 Like

Oh, lol! I forgot about the first argument :rofl:

Thanks!

No worries, glad I could provide you with a solution. Good luck with your game.