I’m trying to save a table using :JSONEncode() and :JSONDecode(). The loading of data works just fine, but the saving doesn’t work, the pcall doesn’t print anything, even errors.
Here’s the saving code
game.Players.PlayerRemoving:connect(function(plr)
local inv = {}
for i, v in pairs(plr.Values.Plot.Value.Dirt:GetChildren()) do
inv[v.Name] = v.Dirt.Value
inv[v.Name.."WeightMin"] = v.Dirt.WeightRange.Min.Value
inv[v.Name.."WeightMax"] = v.Dirt.WeightRange.Max.Value
end
local success, errormessage = pcall(function()
ds:SetAsync(plr.UserId.."dirt", https:JSONEncode(inv))
-- nothing in here prints
end)
if success then
print("Saved") -- not printing
else
warn(errormessage) -- not printing
end
end)
Picture of the dirt object tree
Here’s loading code
local newData
local success, errorMessage = pcall(function()
newData = ds:GetAsync(plr.UserId.."dirt")
end)
if success then
print("Loaded")
else
warn(errorMessage)
end
if newData then
local dt = https:JSONDecode(newData)
for i, v in pairs(dt) do
local dir = cl.Parent.Parent.Dirt:FindFirstChild(i)
if dir then
dir.Dirt.Value = v
for o, c in pairs(dt) do
if o == dir.Name.."WeightMin" then
dir.Dirt.WeightRange.Min.Value = c
elseif o == dir.Name.."WeightMax" then
dir.Dirt.WeightRange.Max.Value = c
end
end
end
end
end
Does this “ds:SetAsync(plr.UserId…“dirt”, https:JSONEncode(inv))” code part even run? or the entire pcall function is just existing but not working or just the results are the problem?
game.Players.PlayerRemoving:Connect(function(plr)
local inv = {}
for i, v in pairs(plr.Values.Plot.Value.Dirt:GetChildren()) do
inv[v.Name] = v.Dirt.Value
inv[v.Name.."WeightMin"] = v.Dirt.WeightRange.Min.Value
inv[v.Name.."WeightMax"] = v.Dirt.WeightRange.Max.Value
end
print("hello") -- hello
local success, errormessage = pcall(function()
ds:SetAsync(plr.UserId.."dirt", https:JSONEncode(inv))
print("hello") -- nothing
end)
if success then
print("Saved")
else
warn(errormessage)
end
end)
You can use game:BindToClose() in any script to execute some code before the game closes, which could happen when the final player leaves. You can add any code and the game will delay the closure of the server with 30 seconds max to execute your code. Of course, you don’t need the full 30 seconds so maybe just try: