Data not saving

I’m usually super pro in Data storing, but now it just doesn’t work?

local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Lighting = game:GetService("Lighting")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Argument = ReplicatedStorage:WaitForChild("Argument")

local Key = "NotOfficialYetxx" --DO NOT CHANGE THIS.

local Multiplier = 1
local maximumBackpackCapacity 

local DataStoreService = game:GetService("DataStoreService")
local Database = DataStoreService:GetDataStore(Key)

--Lighting
for _, v in pairs(Lighting:GetChildren()) do
	if v.Name == "Bloom" or v.Name == "ColorCorrection" or v.name == "DepthOfField" or v.Name == "SunRays" then
		v.Enabled = true
	end
end

--Players
Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		
		local leaderstats = Instance.new("Folder")
		leaderstats.Parent = Player
		leaderstats.Name = "leaderstats"

		local Candy = Instance.new("NumberValue")
		Candy.Parent = leaderstats
		Candy.Name = "Candy"

		local Money = Instance.new("NumberValue")
		Money.Parent = leaderstats
		Money.Name = "Money"
		
		local BackpackFolder = Instance.new("Folder")
		BackpackFolder.Parent = Player
		BackpackFolder.Name = "BackpackFolder"
		
		local Backpack1 = Instance.new("BoolValue")
		Backpack1.Parent = BackpackFolder
		Backpack1.Name = "Backpack1"
		Backpack1.Value = true
		
		local Backpack2 = Instance.new("BoolValue")
		Backpack2.Parent = BackpackFolder
		Backpack2.Name = "Backpack2"
		Backpack2.Value = false
		
		local Backpack3 = Instance.new("BoolValue")
		Backpack3.Parent = BackpackFolder
		Backpack3.Name = "Backpack3"
		Backpack3.Value = false
		
		local Backpack4 = Instance.new("BoolValue")
		Backpack4.Parent = BackpackFolder
		Backpack4.Name = "Backpack4"
		Backpack4.Value = false
		
		local candyData
		local moneyData
		
		local backpack1Data
		local backpack2Data
		local backpack3Data
		local backpack4Data
		
		local s, e = pcall(function()
			candyData = Database:GetAsync(Player.UserId.."-Candy")
			moneyData = Database:GetAsync(Player.UserId.."-Money")
			backpack1Data = Database:GetAsync(Player.UserId.."-Backpack1")
			backpack2Data = Database:GetAsync(Player.UserId.."-Backpack2")
			backpack3Data = Database:GetAsync(Player.UserId.."-Backpack3")
			backpack4Data = Database:GetAsync(Player.UserId.."-Backpack4")
		end)
		
		if s and not e then
			
			Candy.Value = candyData
			Money.Value = moneyData
			
			if Backpack1.Value == true then
				maximumBackpackCapacity = 10
			elseif Backpack2.Value == true then
				maximumBackpackCapacity = 100
			elseif Backpack3.Value == true then
				maximumBackpackCapacity = 1000
			elseif Backpack4.Value == true then
				maximumBackpackCapacity = 10000
			end
			
		elseif e and not s then
			warn(e)
		end
		
		--SetGuiValues
		Argument:FireClient(Player, "ChangeDataValues")
		Argument:FireClient(Player, "ChangeDataValues", maximumBackpackCapacity)
		
		Candy:GetPropertyChangedSignal("Value"):Connect(function()
			if Candy.Value >= maximumBackpackCapacity then
				Candy.Value = maximumBackpackCapacity
				Argument:FireClient(Player, "ChangeDataValues", maximumBackpackCapacity)
			end
		end)
		
	end)
end)

Players.PlayerRemoving:Connect(function(Player)
	local s, e = pcall(function()
		--Saving candy and money
		Database:SetAsync(Player.UserId.."-Candy", Player.leaderstats.Candy.Value)
		Database:SetAsync(Player.UserId.."-Money", Player.leaderstats.Money.Value)
		--Saving the backpacks
		Database:SetAsync(Player.UserId.."-Backpack1", Player.BackpackFolder.Backpack1.Value)
		Database:SetAsync(Player.UserId.."-Backpack2", Player.BackpackFolder.Backpack2.Value)
		Database:SetAsync(Player.UserId.."-Backpack3", Player.BackpackFolder.Backpack3.Value)
		Database:SetAsync(Player.UserId.."-Backpack4", Player.BackpackFolder.Backpack4.Value)
	end)
	if e and not s then
		warn(e)
	end
end)

game:BindToClose(function()
	for _, v in pairs(Players:GetPlayers()) do
		Database:SetAsync(v.UserId.."-Candy", v.leaderstats.Candy.Value)
		Database:SetAsync(v.UserId.."-Money", v.leaderstats.Money.Value)
		Database:SetAsync(v.UserId.."-Backpack1", v.BackpackFolder.Backpack1.Value)
		Database:SetAsync(v.UserId.."-Backpack2", v.BackpackFolder.Backpack2.Value)
		Database:SetAsync(v.UserId.."-Backpack3", v.BackpackFolder.Backpack3.Value)
		Database:SetAsync(v.UserId.."-Backpack4", v.BackpackFolder.Backpack4.Value)
	end
end)

ANy help is appreciated! Thanks

1 Like

have you tried

maybe reduce the use of SetAsync

1 Like

My game has full access to every Security API. I know 100% how to use :SetAsync().

that is good for you but data store service has limits
maybe use tables

1 Like

Mhm, I know DataStoreService has it’s limitations. Never found myself using tables to be honest, but I could try…?

You’re trying to use a single datastore to store a lot of values, instead create an array. Also, be careful of DataStore limits and data loss, if you’re having problems with this try using DataStore2 or ProfileService.

local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Lighting = game:GetService("Lighting")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Argument = ReplicatedStorage:WaitForChild("Argument")

local Key = "NotOfficialYetxx" --DO NOT CHANGE THIS.

local Multiplier = 1
local maximumBackpackCapacity 

local DataStoreService = game:GetService("DataStoreService")
local Database = DataStoreService:GetDataStore(Key)

--Lighting
for _, v in ipairs(Lighting:GetChildren()) do
	if v.Name == "Bloom" or v.Name == "ColorCorrection" or v.name == "DepthOfField" or v.Name == "SunRays" then
		v.Enabled = true
	end
end

local function SaveData(Player)
	local Leaderstats, BackpackFolder = Player.leaderstats, Player.BackpackFolder
	local Values = {
		Leaderstats.Candy.Value;
		Leaderstats.Money.Value;
		BackpackFolder.Backpack1.Value;
		BackpackFolder.Backpack2.Value;
		BackpackFolder.Backpack3.Value;
		BackpackFolder.Backpack4.Value
	}
	
	pcall(function()
		Database:SetAsync(Player.UserId, Values)
	end)
end

--Players
Players.PlayerAdded:Connect(function(Player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = Player
	leaderstats.Name = "leaderstats"

	local Candy = Instance.new("NumberValue")
	Candy.Parent = leaderstats
	Candy.Name = "Candy"

	local Money = Instance.new("NumberValue")
	Money.Parent = leaderstats
	Money.Name = "Money"

	local BackpackFolder = Instance.new("Folder")
	BackpackFolder.Parent = Player
	BackpackFolder.Name = "BackpackFolder"

	local Backpack1 = Instance.new("BoolValue")
	Backpack1.Parent = BackpackFolder
	Backpack1.Name = "Backpack1"
	Backpack1.Value = true

	local Backpack2 = Instance.new("BoolValue")
	Backpack2.Parent = BackpackFolder
	Backpack2.Name = "Backpack2"
	Backpack2.Value = false

	local Backpack3 = Instance.new("BoolValue")
	Backpack3.Parent = BackpackFolder
	Backpack3.Name = "Backpack3"
	Backpack3.Value = false

	local Backpack4 = Instance.new("BoolValue")
	Backpack4.Parent = BackpackFolder
	Backpack4.Name = "Backpack4"
	Backpack4.Value = false

	local Data

	local s, e = pcall(function()
		Data = Database:GetAsync(Player.UserId) or { }
	end)

	if s then
		pcall(function()
			Candy.Value = Data[1] or 0
			Money.Value = Data[2] or 0

			Backpack1.Value = Data[3] or false
			Backpack2.Value = Data[4] or false
			Backpack3.Value = Data[5] or false
			Backpack4.Value = Data[6] or false

			if Backpack1.Value then
				maximumBackpackCapacity = 10
			elseif Backpack2.Value then
				maximumBackpackCapacity = 100
			elseif Backpack3.Value then
				maximumBackpackCapacity = 1000
			elseif Backpack4.Value then
				maximumBackpackCapacity = 10000
			end
		end)
	end

	--SetGuiValues
	Argument:FireClient(Player, "ChangeDataValues")
	Argument:FireClient(Player, "ChangeDataValues", maximumBackpackCapacity)

	Candy:GetPropertyChangedSignal("Value"):Connect(function()
		if Candy.Value >= maximumBackpackCapacity then
			Candy.Value = maximumBackpackCapacity
			Argument:FireClient(Player, "ChangeDataValues", maximumBackpackCapacity)
		end
	end)
end)

Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function()
	for _, v in ipairs(Players:GetPlayers()) do
		SaveData(v)
	end
end)

Just for information:

image

If I were to add more values, I just count up? (e.g. Data[7], Data[8], etc etc)?

Yes, this is the order of the array values which is counted with numbers from up to down (Array[index]).

1 Like

I found a little “game-breaking” issue.

image

The error is somewhere in this code-block.

	if s then
		pcall(function()
			Candy.Value = Data[1] or 0
			Money.Value = Data[2] or 0

			Backpack1.Value = Data[3] or false
			Backpack2.Value = Data[4] or false
			Backpack3.Value = Data[5] or false
			Backpack4.Value = Data[6] or false

			if Backpack1.Value then
				maximumBackpackCapacity = 10
			elseif Backpack2.Value then
				maximumBackpackCapacity = 100
			elseif Backpack3.Value then
				maximumBackpackCapacity = 1000
			elseif Backpack4.Value then
				maximumBackpackCapacity = 10000
			end
			
			Check = true
		end)
	else
		warn(e)
	end

My bad, I forgot to put the player’s id on the :GetAsync() calling function.

Database:GetAsync(Player.UserId) or { } should work.

Worked! Thank you so much for your quick assisting and thinking! Have a wonderful day

1 Like