Datastore 2 not saving?

local data = {}

local HttpService = game:GetService("HttpService")
local players = game:GetService("Players")

local Datastore2 = require(1936396537) -- Get datastore2 module
Datastore2.Combine("Inventory", "Gold", "Armour", "Potions", "Weapons","Health","Damage","MagicDamage","Level", "Experience","MaxExperience" )

data.AddedEvent = function(player)
	
	
	--// Inventory System
	local Inventory = Instance.new("Folder", player); Inventory.Name = "Inventory"
	local playerGold = Instance.new("IntValue", Inventory); playerGold.Name = "Gold"
    local playerArmour = Instance.new("StringValue", Inventory); playerArmour .Name = "Armour"
    local playerPotions = Instance.new("StringValue", Inventory); playerPotions .Name = "Potions"
	local playerWeapons = Instance.new("StringValue", Inventory); playerWeapons .Name = "Weapons"
	
	--// Player Stats
	local playerHealth = Instance.new("IntValue", Inventory); playerHealth.Name = "Health"
	local playerDamage = Instance.new("IntValue", Inventory); playerDamage.Name = "Damage"
	local playerMagicDmg = Instance.new("IntValue", Inventory); playerMagicDmg.Name = "MagicDamage"
	
	--// Players Level
	local LevelSystem = Instance.new("Folder", player); LevelSystem.Name = "LevelSystem"
	local PlayerLevel = Instance.new("IntValue", LevelSystem); PlayerLevel.Name = "Level"
	local PlayerXp = Instance.new("IntValue", LevelSystem); PlayerXp.Name = "Experience"
	local PlayerMaxXp = Instance.new("IntValue", LevelSystem); PlayerMaxXp.Name = "MaxExperience" 
	
	--// Datastore 
    local goldStore = Datastore2("Gold", player); local goldStoreData = goldStore:Get()
    local armourStore = Datastore2("Armour", player); local armourStoreData = armourStore:Get({ })
    local potionsStore = Datastore2("Potions", player); local potionsStoreData = potionsStore:Get({ })
	local weaponsStore= Datastore2("Weapons", player); local weaponsStoreData = weaponsStore:Get({ })
	local healthStore = Datastore2("Health", player); local healthStoreData = healthStore:Get()
	local damageStore = Datastore2("Damage", player); local damageStoreData = damageStore:Get()
	local magicStore = Datastore2("MagicDamage", player); local magicStoreData = magicStore:Get()
	local levelStore = Datastore2("Level", player); local levelStoreData = levelStore:Get()
	local xpStore = Datastore2("Experience", player); local XpStoreData = xpStore:Get()
	local maxxpStore = Datastore2("MaxExperience",player); local MaxXpStoreData = maxxpStore:Get()
	
	--// JSON Encode/Decode
    playerArmour.Value = HttpService:JSONEncode(armourStoreData) -- Turning your items into a JSON table. we don't need to do this for gold as gold is a number and wont have multiple items inside
    armourStore:OnUpdate(function(decodedData)
         playerArmour.Value = HttpService:JSONEncode(decodedData) -- Encodes newly updated data
	end)

    playerPotions.Value = HttpService:JSONEncode(potionsStoreData) 
    potionsStore:OnUpdate(function(decodedData)
         playerPotions.Value = HttpService:JSONEncode(decodedData) -- Encodes newly updated data
	end)
    playerWeapons.Value = HttpService:JSONEncode(weaponsStoreData) 
    weaponsStore:OnUpdate(function(decodedData)
         playerWeapons.Value = HttpService:JSONEncode(decodedData) -- Encodes newly updated data
	end)
	
end

return data

This is a dataStore2 Module script and I have it being called from a serverscriptservice, and for some reason when I leave the game my values don’t save? It says Inventory saved but nothing really does save, I also have a boolValue in serverStorage called SaveInStudio, set to true but Nothing saves, I usually change my values on the server and then leave to see if they save but they dont. Any reason or am I just being stupid right now. Below is the serverSriptService script.

local players = game:GetService("Players")

local data = require(game.ServerStorage:WaitForChild("Modules"):WaitForChild("DataStoreHandler"))

players.PlayerAdded:Connect(function(player)

        data.AddedEvent(player)

end)

Could it be related to this ongoing bug?

Others have reported issues with DataStore2 on that thread. Are you getting any of the errors listed on that thread?

2 Likes

Why are you involving HttpService and JSONEncode here???
Not necessary for tables to be encoded before being saved, or even to encode and then decode values each time the value in the store changes.

Also, this isn’t the latest version since the module isn’t maintained on the website, get it from https://github.com/Kampfkarren/Roblox/releases/download/ds2-1.3.1/DataStore2.rbxmx.

Where would I have the HttpSevice and JSONEcode? I recently asked this on my recent post and someone sent me this, I also tried the datastore2 you linked, still doesn’t work, also Isn’t require(1936396537) always the updated version? Sorry for asking a lot of questions.

No it isn’t, kampfkarren will probably have to change or remove that comment (they know about it through the main thread), you don’t really need JSONEncode here because when you attempt to store tables they get encoded into strings internally since you can only save strings to the database.

Edit: You don’t even need this here, there’s a reason why JSONEncode isn’t a function of DataStoreService.

You can save tables just simply by

DataStore:SetAsync(key, {})

Edit 2:

yes, that’s right.

So if I have a weapon with stats, lets say a sword has 12 damage and you upgrade it into 14 damage or something like that, wouldn’t it be better to use JSONEncode, or no could I just save it in a table somehow.

So if I do something like this:

local player_save_table = {
	['EquippedSword'] = Default,
	['Damage'] = 10,

Would this be similar say I have a sword and upgrade would I have something like this which saves the value of the sword.

I don’t get errors, when I leave it says, Player left, inventory saved.

1 Like