Loading with GetAsync not working

I’m trying to save/load data in my game with :JSONEncode() and :JSONDecode() and the saving works just fine but when i load the JSON it prints out as nil

Saving Code

game.Players.PlayerRemoving:Connect(function(plr)
	local inv = {}

	for i, v in pairs(plr.TreeSlots:GetChildren()) do
		inv[v.Name] = v.Value
	end
	for i, v in pairs(plr.Upgrades:GetChildren()) do
		inv[v.Name] = v.Value
	end
	print(inv)

	local success, errormessage = pcall(function()
		ds:SetAsync(plr.UserId.."-fg", https:JSONEncode(inv))
		ds:SetAsync(plr.UserId.."-m", plr.leaderstats.Money.Value)
		print(https:JSONEncode(inv))
	end)

	if success then
		print(plr.Name.." save: Successful")
	else
		print(plr.Name.." save: Unsuccessful")
		warn(errormessage)
	end
end)

What this does is make a table and loop through the items to add it to the table, the table then gets JSON Encoded and gets saved.

Loading Code (This is all in PlayerAdded function)

local newData
local dm
local success, errorMessage = pcall(function()
	newData = ds:GetAsync(plr.UserId.."fg")
	dm = ds:GetAsync(plr.UserId.."m")
end)
	
if success then
	m.Value = dm
	print("Loaded") -- Loaded
    print(newData) -- nil  (table)
    print(dm) -- nil (Money)
else
	warn(errorMessage)
end
	
if newData then
	print("Yes") -- not printing
	local dt = https:JSONDecode(newData)
	for i, v in pairs(dt) do
		print(i, v)
		local tss = ts:FindFirstChild(i)
		if tss then
			tss.Value = v
		else
			local ups = up:FindFirstChild(i)
			if ups then
				ups.Value = v
			end
		end
	end
end

I’ve looked over this multiple times and the data always returns as nil, and this code has worked in other games that I have made, but in this one it suddenly stopped working. If there is anything I forgot to check please tell me, thank you.

Are there any errors and are API services enabled?

There are no errors, I have API and HTTPS enabled

does it print everything corectly or does it not save properly?

It doesn’t load properly, it saves just fine

is the problem here? -------------------------------------

maybe you should loop the datastore reading multiple times

My propblem is that the table that i saved and the money are printed as nil

if success then
	m.Value = dm
	print("Loaded") -- Loaded
    print(newData) -- nil  (table)
    print(dm) -- nil (Money)
else
	warn(errorMessage)
end

When loading, you forgot the “-” on fg and m.

1 Like
local newData
local dm
local success, errorMessage = pcall(function()
	newData = ds:GetAsync(plr.UserId.."-fg")
	dm = ds:GetAsync(plr.UserId.."-m")
end)
2 Likes

It works, i cant believe how little of a mistake that was, thank you sir

No problem! I make these kinds of mistakes all the time :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.