Datastore losing stats and giving stats

I think i allready know what is wrong, but i cant put my finger on it.

My datastore has been giving out stats, and deleting stats randomly, its gamebreaking and it sucks.

Any help will be appreciated.

Code -

local ds = game:GetService("DataStoreService")
local ds1 = ds:GetDataStore("CashSaveSystem1")
local ds2 = ds:GetDataStore("RebirthSaveSystem1")

game.Players.PlayerAdded:connect(function(player)
	local fold = Instance.new("Folder", player)
	fold.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", fold)
	cash.Name = "Dust"
	
	cash.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, cash.Value)
		
	local rebirths = Instance.new("IntValue", fold)
	rebirths.Name = "Resets"
		
	rebirths.Value = ds2:GetAsync(player.UserId) or 0
	ds2:SetAsync(player.UserId, rebirths.Value)
	
	cash.Changed:connect(function()
		ds1:SetAsync(player.UserId, cash.Value)
	end)
	
	rebirths.Changed:connect(function()
		ds2:SetAsync(player.UserId, rebirths.Value)
	end)
end)


game.ReplicatedStorage.Reset.OnServerEvent:connect(function(player)
	if player.leaderstats.Dust.Value >= 500000 then
		player.leaderstats.Dust.Value = 0
		player.leaderstats.Resets.Value = player.leaderstats.Resets.Value + 1
		game.ReplicatedStorage.Reset:FireClient(player)
	end
end)

Your leader stats seems very complicated. You should use a pcall function when calling SetAsync(). You can check out AlvinBlox’s DataStore tutorial. It’s much more easier.

2 Likes

I would, but i dont know how to add 2 leaderstats and save them.

So just 1 thing

What do you mean by

sorry im just a bit confused

You could just use one DataStore and save everything in it. DataStores are like dictionaries.

Sorry if im not being clear, i mean alvin blox’s tutorial only shows you how to do 1 data save, E.G -
Cash, but i want cash and resets

Oh well here’s a couple things

You dont need multiple Data stores(At least not for what your doing)
You can use Tables
so Heres the edits i would apply to your script (Also what im about to tell you might get a bit complex but your using data stores so you should know the basics)
Lets take your Script and make some changes ill comment the changes ive made
Ill leave links at the bottom

local ds = game:GetService("DataStoreService")
local GPD = ds:GetDataStore("GeneralPlayerData") -- You can name the Variable and the DataStroe name whatever you want

game.Players.PlayerAdded:connect(function(player)
	local fold = Instance.new("Folder") -- Its Recommended to not use the Parent Argument to Instance.new its slower whats in my edit 
	fold.Name = "leaderstats"
    fold.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Dust"
    cash.Parent = fold
	
    local Data = GPD:GetAsync(player.UserId)  -- Edit here is that GetAsync() should be wrapped in a pcall but we will leave that out for now
	cash.Value = Data.Cash or 0 
	ds1:SetAsync(player.UserId, cash.Value) -- SetAsync() Should be wrapped in a pcall
		
	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Resets"
    rebirths.Parent = fold
		
	rebirths.Value = Data.Rebirths or 0
	ds2:SetAsync(player.UserId, rebirths.Value) -- Again Should be wrapped in a pcall
	
	cash.Changed:connect(function()
        local SaveTable = {
            Cash = cash.Value;
            Rebirths = Rebirths.Value:
		GPD:SetAsync(player.UserId, cash.Value) -- pcall again but you shouldnt use .Changed as data stores have limits and will be easily broken by using this but since its for testing purposes ill let it stay also i changed the Data Store
	end)
	
    -- I removed the Second 1 since they are combined
end)


game.ReplicatedStorage.Reset.OnServerEvent:connect(function(player)
	if player.leaderstats.Dust.Value >= 500000 then
		player.leaderstats.Dust.Value = 0
		player.leaderstats.Resets.Value = player.leaderstats.Resets.Value + 1
		game.ReplicatedStorage.Reset:FireClient(player)
	end
end)

Tables Link

I cant find the link to the post which explains why you shouldn’t use Instance.new(“IntValue”, player) just dont its slower

pcalls if you want to add them its 10-11 functions down

Data Store Errors And limits

DM me if anything went wrong or if you want a better explanation

1 Like

Check if your’re saving a nil value anywhere by printing all the values after setting ASync