Datastore2 Isn't Saving My Data

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? i want to know the reason that why my data isn’t saving

  2. What is the issue? i’ve no idea why my data isn’t saving

  3. What solutions have you tried so far? (i’m new to datastore2. please, look into my code below)
    Thanks for your help!

local Players = game:GetService('Players')
local ServerScriptService = game:GetService('ServerScriptService')
local DataStore2 = require(ServerScriptService.DataStore2)
DataStore2.Combine("DATA_test",
	"items","level","elims","xp","chips","knife","gun","pet","ability","emote","particle","gears")
local itemsRS = game.ReplicatedStorage.Items
local weaponsRS = itemsRS.Weapons
local abilitiesRS = itemsRS.Abilities
local petsRS = itemsRS.Pets
local gearsRS = itemsRS.Gears
local emotesRS = itemsRS.Emotes
local particlesRS = itemsRS.Particles

Players.PlayerAdded:Connect(function(player)
	-- FOLDERS --
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	local statsFolder = Instance.new("Folder",player)
	statsFolder.Name = "stats"
	local eqGears = Instance.new("Folder",statsFolder)
	eqGears.Name = "EquippedGears"
	local inventory = Instance.new("Folder",player)
	inventory.Name = "Inventory"
	local weaponFolder = Instance.new("Folder",inventory)
	weaponFolder.Name = "Weapons"
	local abilityFolder = Instance.new("Folder",inventory)
	abilityFolder.Name = "Abilities"
	local petFolder = Instance.new("Folder",inventory)
	petFolder.Name = "Pets"
	local gearFolder = Instance.new("Folder",inventory)
	gearFolder.Name = "Gears"
	local emoteFolder = Instance.new("Folder",inventory)
	emoteFolder.Name = "Emotes"
	local particleFolder = Instance.new("Folder",inventory)
	particleFolder.Name = "Particles"

	-- DATASTORES --
	local itemsDS = DataStore2("items",player)
	local levelDS = DataStore2("level",player)
	local elimsDS = DataStore2("elims",player)
	local xpDS = DataStore2("xp",player)
	local chipsDS = DataStore2("chips",player)
	local knifeDS = DataStore2("knife",player)
	local gunDS = DataStore2("gun",player)
	local petDS = DataStore2("pet",player)
	local abilityDS = DataStore2("ability",player)
	local emoteDS = DataStore2("emote",player)
	local particleDS = DataStore2("particle",player)
	local gearsDS = DataStore2("gears",player)

	-- VALUES --
	local Level = Instance.new("IntValue", leaderstats)
	Level.Name = "Level"
	Level.Value = levelDS:Get(1)
	levelDS:OnUpdate(function(newVal)
		Level.Value = newVal
	end)
	local Elims = Instance.new("IntValue", leaderstats)  
	Elims.Name = "Elims"
	Elims.Value = elimsDS:Get(0)
	elimsDS:OnUpdate(function(newVal)
		Elims.Value = newVal
	end)
	local Streak = Instance.new("IntValue", leaderstats) -- never gonna give you up~
	Streak.Name = "Streak" -- never gonna save it down~
	Streak.Value = 0
	local XP = Instance.new("NumberValue", statsFolder)  
	XP.Name = "XP"
	XP.Value = 0
	xpDS:OnUpdate(function(newVal)
		XP.Value = newVal
	end)
	local Chips = Instance.new("NumberValue",statsFolder)  
	Chips.Name = "Chips"
	Chips.Value = 0
	chipsDS:OnUpdate(function(newVal)
		Chips.Value = newVal
	end)
	local Knife = Instance.new("StringValue",statsFolder)
	Knife.Name = "Knife"
	Knife.Value = "De Knife"
	knifeDS:OnUpdate(function(newVal)
		Knife.Value = newVal
	end)
	local Gun = Instance.new("StringValue",statsFolder)
	Gun.Name = "Gun"
	Gun.Value = "De Pistol"
	gunDS:OnUpdate(function(newVal)
		Gun.Value = newVal
	end)
	local Pet = Instance.new("StringValue",statsFolder)
	Pet.Name = "Pet"
	Pet.Value = "None"
	petDS:OnUpdate(function(newVal)
		Pet.Value = newVal
	end)
	local Ability = Instance.new("StringValue",statsFolder)
	Ability.Name = "Ability"
	Ability.Value = "None"
	abilityDS:OnUpdate(function(newVal)
		Ability.Value = newVal
	end)
	local Emote = Instance.new("StringValue",statsFolder)
	Emote.Name = "Emote"
	Emote.Value = "None"
	emoteDS:OnUpdate(function(newVal)
		Emote.Value = newVal
	end)
	local Particle = Instance.new("StringValue",statsFolder)
	Particle.Name = "Particle"
	Particle.Value = "None"
	particleDS:OnUpdate(function(newVal)
		Particle.Value = newVal
	end)

	local INV = {
		Weapons = {},
		Abilities = {},
		Pets = {},
		Gears = {},
		EquippedGears = {},
		Emotes = {},
		Particles = {},
	}
	for _, items in pairs(player.Inventory.Weapons:GetChildren()) do
		table.insert(INV.Weapons, items.Name)
	end
	player.Inventory.Weapons.ChildAdded:Connect(function(newChild)
		table.insert(INV.Weapons, newChild.Name)
	end)
	
	for _, items in pairs(player.Inventory.Abilities:GetChildren()) do
		table.insert(INV.Abilities, items.Name)
	end
	player.Inventory.Abilities.ChildAdded:Connect(function(newChild)
		table.insert(INV.Abilities, newChild.Name)
	end)
	
	for _, items in pairs(player.Inventory.Pets:GetChildren()) do
		table.insert(INV.Pets, items.Name)
	end
	player.Inventory.Pets.ChildAdded:Connect(function(newChild)
		table.insert(INV.Pets, newChild.Name)
	end)
	
	for _, items in pairs(player.Inventory.Gears:GetChildren()) do
		table.insert(INV.Gears, items.Name)
	end
	player.Inventory.Gears.ChildAdded:Connect(function(newChild)
		table.insert(INV.Gears, newChild.Name)
	end)
	
	for _, items in pairs(player.stats.EquippedGears:GetChildren()) do
		table.insert(INV.EquippedGears, items.Name)
	end
	player.stats.EquippedGears.ChildAdded:Connect(function(newChild)
		table.insert(INV.EquippedGears, newChild.Name)
	end)
	
	for _, items in pairs(player.Inventory.Emotes:GetChildren()) do
		table.insert(INV.Emotes, items.Name)
	end
	player.Inventory.Emotes.ChildAdded:Connect(function(newChild)
		table.insert(INV.Emotes, newChild.Name)
	end)
	
	for _, items in pairs(player.Inventory.Particles:GetChildren()) do
		table.insert(INV.Particles, items.Name)
	end
	player.Inventory.Particles.ChildAdded:Connect(function(newChild)
		table.insert(INV.Particles, newChild.Name)
	end)
	
	itemsDS:GetTable(INV) -- Loads
	game.Players.PlayerRemoving:Connect(function()
		itemsDS:Set(INV) -- Saves
	end)

	for _, SavedItems in pairs(INV.Weapons) do
		if weaponsRS:FindFirstChild(SavedItems) then
			weaponsRS[SavedItems]:Clone().Parent = weaponFolder
		end
	end
	for _, SavedItems in pairs(INV.Abilities) do
		if abilitiesRS:FindFirstChild(SavedItems) then
			abilitiesRS[SavedItems]:Clone().Parent = abilityFolder
		end
	end
	for _, SavedItems in pairs(INV.Pets) do
		if petsRS:FindFirstChild(SavedItems) then
			petsRS[SavedItems]:Clone().Parent = petFolder
		end
	end
	for _, SavedItems in pairs(INV.Emotes) do
		if emotesRS:FindFirstChild(SavedItems) then
			emotesRS[SavedItems]:Clone().Parent = emoteFolder
		end
	end
	for _, SavedItems in pairs(INV.Gears) do
		if gearsRS:FindFirstChild(SavedItems) then
			gearsRS[SavedItems]:Clone().Parent = gearFolder
		end
	end
	for _, SavedItems in pairs(INV.EquippedGears) do
		if gearsRS:FindFirstChild(SavedItems) then
			gearsRS[SavedItems]:Clone().Parent = eqGears
		end
	end
	for _, SavedItems in pairs(INV.Particles) do
		if particlesRS:FindFirstChild(SavedItems) then
			particlesRS[SavedItems]:Clone().Parent = particleFolder
		end
	end
	
	game.ReplicatedStorage.Items.Weapons["De Knife"]:Clone().Parent = player.Inventory.Weapons
	game.ReplicatedStorage.Items.Weapons["De Pistol"]:Clone().Parent = player.Inventory.Weapons
	while true do
		wait(180)
		itemsDS:Set(INV)
	end
end)
2 Likes

So I think it is in how you are using it. You have an infinite loop in your PlayerAdded that is going to keep setting your store back to the values in that function to whatever INV is every 180 seconds effectively wiping out any saves you make elsewhere. You should only read in the values with Get in your PlayerAdded. You shouldn’t need an infinite loop to save either. Wherever you change the values you would use your datastore and use the Set to change the value.

Also the DataStore2 automatically saves player data when they leave the game so there is no need to use the PlayerRemoving as it is built in.

2 Likes

oh thanks, but where do I put itemsDS:Set(INV) though.

and how do I give player tools only when they first joined?

if i’m correct it’s only save when i use something like:
DataStore2("elims", player):Increment(1)
but it won’t save when i tried using:
player.leaderstats.Elims.Value += 1
Numbers value are fine now, but how do i deal with strings and table?

1 Like

Like quakage has said, you should just change values by using :Set() to immediately save the data. You don’t need to save upon player removing or with an infinite loop.

Correct because changing leaderstats values are separate from the DataStore. You can utilize the DataStore event like so:


local function updateElims(updatedValue)
	Elims.Value = updatedValue
end

elimsDS:OnUpdate(updateElims)

I have code like this in my PlayerAdded function so whenever I change the value of elims in any other place in my game it updates the leaderstats as well. This other area of the code could use Increment or Set to change the value.

1 Like