Loading tables via datastore

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    User can submit a note to be saved.
    Note can be accesed in another game/server (aka datastore)

  2. What is the issue? Include screenshots / videos if possible!

Local script inside gui

local temp = script.Template
local function Load()
	local data = game.ReplicatedStorage.Folder["Data Get"]:InvokeServer()
	if data == "False" then
		print("No saved notes")
		script.Parent.TextLabel.Text = "LOAD SAVES // no saved notes"
	else
		print(game:GetService("HttpService"):JSONDecode(data))
		print(data)
		print(#game:GetService("HttpService"):JSONDecode(data))
		print(#data)
		
	
	end

end


wait(1)
Load()

Output >

  15:34:30.408  table: 0x5a03fa272975691f  -  Client - LocalScript:9
  15:34:30.408  {"2":{"Name":"2","Text":"secomd note test text","Desc":"seecondd note"}}  -  Client - LocalScript:10
  15:34:30.408  0  -  Client - LocalScript:11
  15:34:30.409  296  -  Client - LocalScript:12

How could I get the amount of notes, and get the necesary info for my teemplate (image below)

Image

Script for storing notes

Script for storing notes:

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("Notes")
local function saveNote(Player, Name, Desc, Value)
	print(Player, Name, Desc, Value)
	local note = {
		[Name] = {
			Name = Name,
			Desc = Desc,
			Text = Value
		}
		}
	dataStore:SetAsync(Player.UserId, game:GetService("HttpService"):JSONEncode(note))
end
game.ReplicatedStorage.Folder["Data Store"].OnServerEvent:Connect(saveNote)
Script for getting notes

script for getting notes:

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("Notes")
local function saveNote(p)
	local data = dataStore:GetAsync(p.UserId)
	if data ~= nil then
		return data
	else
		return "False"
	end	
end
game.ReplicatedStorage.Folder["Data Get"].OnServerInvoke = saveNote

One more issue with storing notes, it doesnt add another note but just replaces the current one.

For the saving function, I would have the client pass a table of all of the notes and save it from there. Also, you might as well remove the original [name] = and just leave that as a blank so it will just set it to a number. Lastly, can we see the code where you send the notes for saving? It would be much easier to help being able to see it.

1 Like

Dictionaries can’t be referenced by size using # as they are key-value pairs and can’t be numerically indexed, so # will return 0 (there’s no indexer to iterate until it finds how big the dictionary is)
To print your data, decode it first, then print:

local notes = game:GetService("HttpService"):JSONDecode(data)
print(notes)

You will want to manually iterate the dictionary and hold your own counter. Something like:

local num = 0
for _, note in pairs(notes) do
    num += 1
end

print(num)
1 Like

Thanks for your reply it did not work, it still replaced the previous note

script.Parent.Save.MouseButton1Click:Connect(function()
	if script.Parent.name.Text ~= "" and script.Parent.Desc.Text ~= "" then
		game.ReplicatedStorage.Folder["Data Store"]:FireServer(script.Parent.name.Text, script.Parent.Desc.Text, script.Parent.Parent.Parent.ScrollingFrame.TextBox.Text)
	end
end)

Hi This works! Thank you, I will mark it as solution as soon as I fix the second problem.

1 Like