Datastore not loading values.`

Sure I’ll try that out, do you know of any posts that explain this?

EDIT: Should I also do this for loading the values to avoid future problems?

1 Like
local MainTable = {}

for i,v in pairs(inventory) do
    local ToolTable ={}
    table.insert(ToolTable,v.Name)
    table.insert(ToolTable,v.Value)
    table.insert(MainTable,ToolTable)
    --Once finished adding all the values. Save outside of this loop
end
--Save code here.



--Getting it

for i,v in pairs(GetAsyncTable) do
    v[1] -- Tool Name
    v[2] -- Value
end
2 Likes

Tysm. I’ll try this out in a bit and let you know how it goes :slight_smile:

2 Likes

By the way, to save it is the same method, except for v.Value its the MainTable?

1 Like

You create a sub table which you will insert into the main table.

local MainTable = {} -- This is what the datastore saves.

for i,v in pairs(inventory) do
    local ToolTable ={} -- Sub table that goes into the main table which is saved on the datastore.
    table.insert(ToolTable,v.Name)
    table.insert(ToolTable,v.Value)
    table.insert(MainTable,ToolTable) -- Inserts subtable into main
    --Once finished adding all the values. Save outside of this loop
end
--Save code here. Saves the Main table.
1 Like

Heres a rewrite of your old one.

function module:SaveItems(player)
	local inventory = game.ServerStorage.PlayerValues[player.UserId].Inventory:GetChildren()
  local SaveTable = {}

	for i,v in pairs(inventory) do
			local ToolTable ={}
      table.insert(ToolTable,v.Name)
      table.insert(ToolTable,v.Value)
      table.insert(SaveTable,ToolTable)
	end
  local success, errormsg = pcall(function()
			invData:SetAsync(player.UserId,SaveTable)
	end)

	if not success then
		warn(errormsg)
	end
end
2 Likes

It’s very likely datastores are throttling, i would highly recommend saving data in tables

3 Likes

Well noting about it being cached, what he said there no, but, your datastores can throttle from all the values you’re saving and getting at the exact same time.

1 Like

Hey I tried this a little bit differently, and still isn’t working.

function module:SaveItems(player)
	local inventory = game.ServerStorage.PlayerValues[player.UserId].Inventory:GetChildren()
	local SaveDict = {
		["Teddy"] = 0,
		["Dragon"] = 0,
		["Panda"] = 0,
		["Bear"] = 0,
		["Deer"] = 0,
		["Pig"] = 0,
		["Monkey"] = 0,
		["Bunny"] = 0,
		["Dog"] = 0,
		["Cat"] = 0
	}


	for i,v in pairs(inventory) do
		local index = SaveDict[v.Name]
		if index then
			index = v.Value
			print(index)
			print("SET")
		end
	end
	local success, errormsg = pcall(function()
		invData:SetAsync(player.UserId,SaveDict)
	end)

	if not success then
		warn(errormsg)
	else
		print("SAVED")
		
		for i,v in pairs(SaveDict) do
			print(SaveDict[v])
		end
	end
end

It prints the correct value, when saving, like a 1 where there should be a one, then “SET”, but loading in it does nothing. I changed with the DataStore Editor, and all values are 0

Is your inventory values set correctly, your script looks fine.

1 Like

Datastore not loading in data has nothing to do with throttling, if they were throttled, the data would load in some time later. Saving tables is a good idea, however they don’t prevent throttling because OP is loading data within a single key.

1 Like

I guess you didn’t think this through.

If data wasn’t loaded, that would mean trying to save unloaded data would cause it overwrite that value with false data.

Anyhow, you should be using tables anyway.

2 Likes

Stop going off topic, that wasn’t my point. My point was to clear out that saving tables don’t stop throttling.

I never said saving tables was a bad idea:

Saving tables is a good idea

1 Like

Change this the v to i.

This wouldn’t work, you need to do inventory[i] = v, that wouldn’t work either because I’m not understanding what you wanna do here, but you’re just setting a variable not the data inside the data.

I guess this works,

for _, entry in ipairs(inventory) do
    SaveDict[entry.Name] = entry.Value
end

Use ipairs when using :GetChildren() and stuff like that. They’re arrays, meaning they consist of just number indexes, pairs() works fine, but ipairs() is SLIGHTLY faster, it isn’t important, it’s just better.

ipairs() is not compatible with custom indexes other than numbers.


Stop saying it’s off topic. It’s not.

If you’re saving tables with every index being a new key, then yes it does stop it if you were having throttling because of that. That’s how your supposed to have inventories and tables in general.

Ok? That doesn’t change that you bringed the most obvious point ever? Of course it wouldn’t “remove the throttling” from DataStores. I’m not that dumb, and I never meant that when I said this:

I said this because his saving method was already saving values using the “every index” method, which should be changed as fast as possible to not cause problems and annoyances in the future like converting data. Not even counting the fact that it would be annoying to comply with GDPR requests.

I’m not answering anything else anyways, it’s 3am at this point and I’m going to sleep, hope you find solutions if anything happens. :sweat_smile:

2 Likes

If you’re saving tables with every index being a new key, then yes it does stop it if you were having throttling because of that. That’s how your supposed to have inventories and tables in general.

Why are you subjectively spreading false information? If you’re saving different values under different keys, it wouldn’t throttle as long as your code is respects the cooldowns:

DataStores would only throttle on saving when you save data under the same key if the cooldown of 6 seconds hasn’t been passed yet.

You’re only correct on the fact that inventories should be saved under a table since it is good practice and will reduce some headaches when loading it, but that won’t stop throttling which is my point here.

I suggest you give this a good read: Data Stores

PS:

For loading data via GetAsync, there won’t be any problems as long as your code isn’t calling it too frequently per minute:

3 Likes

Sorry I wasn’t home all day. I believe it is set correctly, when printing in the for loop it prints the correct value.

1 Like

Trying this causes the value to print out 0 instead of one. :frowning:

1 Like

Can I get a sample of your full code just to take a deeper dive? It’s “still” 4 am hm, I’m awake so I’ll try my best, I guess it’s day for you :sweat_smile:

Yes

Sure, do you want the saving and loading parts?

1 Like

Didn’t see the edits, heres the scripts

function module:LoadItems(player)
	local inventory = game.ServerStorage.PlayerValues[player.UserId].Inventory:GetChildren()
	local Table = nil

	local success, errormsg = pcall(function()
		Table = invData:GetAsync(player.UserId)
	end)

	if success then
		for i,v in ipairs(inventory) do
			if table.find(Table, v.Name) then
				v.Value = Table[v.Name]
			end
		end
	else
		warn(errormsg)
	end
	for i, v in ipairs(inventory) do
		print(v.Value)
		if v.Value > 0 then
			for x = 1, v.Value, 1 do
				print(v.Name)
				local pet = module.Pets[v.Name]
				print(pet.Name)
				module:AddItem(player, pet)
			end
		end
	end
end

function module:SaveItems(player)
	local inventory = game.ServerStorage.PlayerValues[player.UserId].Inventory:GetChildren()
	local SaveDict = {
		["Teddy"] = 0,
		["Dragon"] = 0,
		["Panda"] = 0,
		["Bear"] = 0,
		["Deer"] = 0,
		["Pig"] = 0,
		["Monkey"] = 0,
		["Bunny"] = 0,
		["Dog"] = 0,
		["Cat"] = 0
	}


	for i,v in pairs(inventory) do
		local index = SaveDict[i]
		if index then
			index = v.Value
			print(index)
			print("SET")
		end
	end
	local success, errormsg = pcall(function()
		invData:SetAsync(player.UserId,SaveDict)
	end)

	if not success then
		warn(errormsg)
	else
		print("SAVED")
		
		for i,v in pairs(SaveDict) do
			print(SaveDict[i])
		end
	end
end
1 Like