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
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.
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 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’)