Datastore not saving

Hello and i am trying to make a data store system for my game. I made one, but sometimes i get messages like the data store server que has filled up. I want to know how i can get around this because when this happens it causes a loss of stats. Is their anything i can do to stop this? Is this happening because of a while loop or multiple firing via a player joining / leaving or just an inefficient use of data storing.

script

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlayerData")
local debounce = false
------Stats Data Store----
game.Players.PlayerAdded:Connect(function(Plr)
	local Ability = nil
	
	local stats = Instance.new("Folder", Plr)
	stats.Name = "Stats"
	--- Level System
	local Level = Instance.new("NumberValue", stats)
	Level.Name = "Level"
	Level.Value = 1
	
	local Exp = Instance.new("NumberValue", stats)
	Exp.Name = "Exp"
	Exp.Value = 0
	
	local ExpNeed = Instance.new("IntValue", stats)
	ExpNeed.Name = "ExpNeed"
	ExpNeed.Value = 100
	
	--- Money and Spin System
	local Yen = Instance.new("IntValue", stats)
	Yen.Name = "Yen"
	Yen.Value = 100000
	
	--- Stats System
	local Points = Instance.new("NumberValue", stats)
	Points.Name = "Points"
	Points.Value = 3
	
	local MaxHealth = Instance.new("IntValue", stats)
	MaxHealth.Name = "MaxHealth"
	MaxHealth.Value = 100
	
	local Stamina = Instance.new("IntValue", stats)
	Stamina.Name = "Stamina"
	Stamina.Value = 0
	
	local Quirk = Instance.new("StringValue", stats)
	Quirk.Name = "Abillity"
	Quirk.Value = "Quirkless"
	
	--- Fame
	local leaderstats = Instance.new("Folder",Plr)
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Plr
	
	local Fame = Instance.new("IntValue")
	Fame.Name = "Fame"
	Fame.Value = 0
	Fame.Parent = leaderstats
	
	local Faction = Instance.new("StringValue", stats)
	Faction.Name = "Faction"
	Faction.Value = "Civilian"
	
	local GymTime = Instance.new("IntValue", stats)
	GymTime.Name = "GymTime"
	GymTime.Value = 0
	
	spawn(function()		
		
		wait(4)		
		
		local StaminaBarMax = Instance.new("IntValue",stats)		
		StaminaBarMax.Name = "AbilityStaminaMax"	
		StaminaBarMax.Value =  100 + (Plr.Stats.Stamina.Value*5)			
		
		local StaminaBar = Instance.new("IntValue",stats)	
		StaminaBar.Name = "AbilityStamina"	
		StaminaBar.Value = StaminaBarMax.Value		
	end)
	
	local Strength = Instance.new("IntValue", stats)
	Strength.Name = "Strength"
	Strength.Value = 0
	
	
	local playerdata = {
		Fame = Fame.Value,
		Faction = Faction.Value,
		Quirk = Quirk.Value,
		Stamina = Stamina.Value,
		MaxHealth = MaxHealth.Value,
		Points = Points.Value,
		Yen = Yen.Value,
		ExpNeed = ExpNeed.Value,
		Exp = Exp.Value,
		Level = Level.Value,	
		Strength = Strength.Value,
		GymTime = GymTime.Value,
	}
	
	local data
	local success, errormessage = pcall(function()
		 Data = DataStore:GetAsync(Plr.UserId)
	end)
	
if success then	
	if Data then
		playerdata.Faction = Data.Faction
		playerdata.Fame = Data.Fame
		playerdata.Quirk = Data.Quirk
		playerdata.Stamina = Data.Stamina
		playerdata.MaxHealth = Data.MaxHealth
		playerdata.Points = Data.Points
		playerdata.Yen = Data.Yen
		playerdata.ExpNeed = Data.ExpNeed
		playerdata.Exp = Data.Exp
		playerdata.Level = Data.Level
		playerdata.Strength = Data.Strength
		playerdata.GymTime = Data.GymTime	
	end	
end
	
	Fame.Value = playerdata.Fame
	Quirk.Value = playerdata.Quirk
	Stamina.Value = playerdata.Stamina
	MaxHealth.Value = playerdata.MaxHealth
	Points.Value = playerdata.Points
	Yen.Value = playerdata.Yen
	ExpNeed.Value = playerdata.ExpNeed
	Exp.Value = playerdata.Exp
	Level.Value = playerdata.Level
	Strength.Value = playerdata.Strength
	Faction.Value = playerdata.Faction
	GymTime.Value = playerdata.GymTime
	
	spawn(function()
		while wait(61) do
		Ability = Quirk.Value		
		playerdata.Fame = Fame.Value
		playerdata.Quirk = Ability
		playerdata.Stamina = Stamina.Value
		playerdata.MaxHealth = MaxHealth.Value
		playerdata.Points = Points.Value
		playerdata.Yen = Yen.Value
		playerdata.ExpNeed = ExpNeed.Value
		playerdata.Exp = Exp.Value
		playerdata.Level = Level.Value
		playerdata.Strength = Strength.Value
		playerdata.Faction = Faction.Value	
		playerdata.GymTime = GymTime.Value	
		DataStore:SetAsync(Plr.UserId, playerdata)
		end	
	end)	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerdata = {}
	local Fame = player.leaderstats.Fame
	local stats = player.Stats
	local quirk = stats.Abillity
	local Stamina = stats.Stamina
	local MaxHealth = stats.MaxHealth
	local Points = stats.Points
	local Yen = stats.Yen
	local ExpNeed = stats.ExpNeed
	local Exp = stats.Exp
	local Level = stats.Level
	local Strength = stats.Strength
	local Faction = stats.Faction
	local GymTime = stats.GymTime
	
	playerdata.Fame = Fame.Value
	playerdata.Quirk = quirk.Value
	playerdata.Stamina = Stamina.Value
	playerdata.MaxHealth = MaxHealth.Value
	playerdata.Points = Points.Value
	playerdata.Yen = Yen.Value
	playerdata.ExpNeed = ExpNeed.Value
	playerdata.Exp = Exp.Value
	playerdata.Level = Level.Value
	playerdata.Strength = Strength.Value
	playerdata.Faction = Faction.Value
	playerdata.GymTime = GymTime.Value
	
	local success, errormessage = pcall(function()
		DataStore:SetAsync(player.UserId, playerdata)
	end)
	
	if success then
		print("DataSaved")
	else
		print("Data Failed To Save")
		warn(errormessage)
	end
				
end)

You should use GlobalDataStore:UpdateAsync().

Hmm how would i replace my old ones using that one?

1 Like