Server-Side DataStore Only Saves One Item

Hi. I was creating a leaderstats and some values to fill it, i had “Money”, “Cap”, "XP and “Level” Int-Values but when i saved them using SetAsync only “Money” saves and i couldn’t understand it the Chat-GPT couldn’t fix it:

edit: (And i always get the “Capacity Error” message)

local StarterGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

-- // DataStore // --
local DataStoreService = game:GetService("DataStoreService")
local MoneyStore = DataStoreService:GetDataStore("MoneyStore")
local LevelStore = DataStoreService:GetDataStore("LevelStore")
local XPStore = DataStoreService:GetDataStore("XPStore")
local CapStore = DataStoreService:GetDataStore("CapStore")
local SaveDataTimeOut = (60 + Players.NumPlayers * 10) / 60
-- // DataStore // --

local DevMode = ServerScriptService:FindFirstChild("DevMode")

if DevMode.Value == false then
	StarterGui.Level.Enabled = false
	for _, AllGui in ipairs(StarterGui:GetChildren()) do
		if AllGui:IsA("ScreenGui") then
			AllGui.Enabled = true
		end
	end	
end

Players.PlayerAdded:Connect(function(Player)
	local Key = Player.UserId
	
	workspace.Tractor.Name = workspace.Tractor.Name .. "_" .. Player.UserId
	workspace.M20.Name = workspace.M20.Name .. "_" .. Player.UserId
	
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	
	local GeneralValues = Instance.new("Folder")
	GeneralValues.Name = "GeneralValues"
	GeneralValues.Parent = Player
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = Leaderstats
	
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Parent = GeneralValues
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = Leaderstats
	
	local Cap = Instance.new("IntValue")
	Cap.Name = "Cap"
	Cap.Parent = GeneralValues
	
	local Success, ErrorMessage = pcall(function()
		Level.Value = LevelStore:GetAsync("level_" .. Key)
		XP.Value = XPStore:GetAsync("xp_" .. Key)
		Money.Value = MoneyStore:GetAsync("money_" .. Key)
		Cap.Value = CapStore:GetAsync("cap_" .. Key)
	end)
	
	if Success then
		print("Loaded")
		if GeneralValues:FindFirstChild("Cap").Value < 100 then
			Cap.Value = 100
			warn("Capasity Error")
		end
	else
		warn(ErrorMessage)
		Level.Value = 0
		XP.Value = 0
		Cap.Value = 100
		Money.Value = 20
	end
	
	while wait() do
		SaveDataTimeOut = (60 + Players.NumPlayers * 10) / 60
		
		MoneyStore:SetAsync("money_" .. Key, Player:FindFirstChild("leaderstats"):FindFirstChild("Money").Value)
		LevelStore:SetAsync("level_" .. Key, Player:FindFirstChild("leaderstats"):FindFirstChild("Level").Value)
		XPStore:SetAsync("xp_" .. Key, Player:FindFirstChild("GeneralValues"):FindFirstChild("XP").Value)
		CapStore:SetAsync("cap_" .. Key, Player:FindFirstChild("GeneralValues"):FindFirstChild("Cap").Value)
		
		wait(SaveDataTimeOut)
	end
end)

You’re trying to save the data every 1.16… seconds. Technically this is still in the limit (but not encouraged) with one player and one datastore, but once you add another player or more datastores you quickly run over this limit.
Use a single datastore and only save the data when the player leaves.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.