Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I save my table as a datastore?

I'm trying to make an Arcane Adventures quest system where it saves all your quest and load it in when you rejoin.. but I can't seem to save my table


local dbservice = game:GetService("DataStoreService")
local db = dbservice:GetDataStore("Datastore")
local HttpService = game:GetService("HttpService")
local data = {
    ["quests"] = {
        ["quest1"] = false;
        ["quest2"] = false;
        ["quest2"] = false;
    }
}

game.Players.PlayerAdded:Connect(function(plr)
    local plrdata = db:GetAsync(plr.UserId) or data
local re = data.quests.quest1
     game.ReplicatedStorage.DataTable:FireClient(plr, re)
end)
 
local json = HttpService:JSONEncode(data)
print(json)



game.ReplicatedStorage.DataTable2.OnServerEvent:Connect(function(player)
        local re = data.quests.quest1
    data.quests.quest1 = true
wait(3)
     game.ReplicatedStorage.DataTable:FireClient(player, re)
end)

2 answers

Datstores will automatically save your table if you put it as the second argument in the function db:SetAsync(key,value). Pcalls will be necessary to make sure it saves as datastores will throttle. To make your (and my) job easier i created a semi-useful easier datastore script, that allows you to keep track of requests, and will automatically yield until the throttle is done, then save. Caching isnt, and wont be implemented when using the GetAsync function, but its not too hard to implement a system if you so desire. Another great feature of my datastore is it allows saving of most of the ValueType objects (BoolValue,StringValue,IntValue,etc.), and auto converts a massive folder of them to a table to then be saved! and when you load said table with the GetAsyncc function you can convert it back to an object with another function. I recently added a CompartObjects function that allows you to "modify" values in the folder, but still allow you to add new values that will stay their defaults if the user didnt have a save with the new values in them. Color3's also supported.

If you think you want to use my module, its fairly simple and acts pretty much like the datastoreservice with one extra argument (the datastore itself). Read about it in the script, most likely have to read the actual script also.

Link to my module: Easy Datastore

Encode

Decode

Don't forget the Encoded table is just a string and strings can be datastored

so if you want to enable a quest before it datastores do it in the event

game.Players.PlayerAdded:Connect(function(plr)
	local plrdata = db:GetAsync(plr.UserId) or data
	local re = data.quests.quest1
	game.ReplicatedStorage.DataTable:FireClient(plr, data)
end)

You dont really have to send encoded data

game.ReplicatedStorage.DataTable2.OnServerEvent:Connect(function(player)
        local re = data.quests.quest1
	data.quests.quest1 = true
	local json = HttpService:JSONEncode(data)
	print(json)
	wait(3)
	game.ReplicatedStorage.DataTable:FireClient(player, data)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.