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.