Hello there I got this error: Bad Argument #1 to "pairs" (table expected, got string) so I actually tried to save a IntValue with Name and Value so and if I print the string it says [null] How can I fix that? Line 31 causes the issue after the error^
local DS = game:GetService("DataStoreService") local PetDS = DS:GetDataStore("PetDataStore") local PetsTable = {} local AUTOSAVE_INTERVAL = 60 game.Players.PlayerAdded:Connect(function(Player) local PetsFolder = Instance.new("Folder") PetsFolder.Name = "Pets" PetsFolder.Parent = Player local key = Player.UserId print(key) PetsTable = PetDS:GetAsync(key) if PetsTable then print(PetsTable) game:GetService("HttpService"):JSONDecode(PetsTable) for _,p in pairs (PetsTable) do local In = Instance.new("IntValue") In.Name = p["Name"] In.Parent = PetsFolder In.Value = p["Value"] end end end) game.Players.PlayerRemoving:Connect(function(Player) PetsTable = {} local key = Player.UserId print(key) for _,i in pairs (Player.Pets:GetChildren()) do if i then table.insert(PetsTable,i) PetsTable[i] = {} table.insert(PetsTable[i],"Name") table.insert(PetsTable[i], "Value") PetsTable[i]["Name"] = i.Name PetsTable[i]["Value"] = i.Value print(PetsTable[i]["Name"]) end end PetDS:SetAsync(key, game:GetService("HttpService"):JSONEncode(PetsTable)) end)
Thanks for answers!
Instead of just game:GetService("HttpService"):JSONDecode(PetsTable)
do PetsTable = game:GetService("HttpService"):JSONDecode(PetsTable)
JSONDecode doesn't change the value, it just returns the new one