Help with Stats

Hi. I’m trying to make stats, but I’m not sure if this is the most efficient way to do it. Can you help me?

local ds = game:GetService("DataStoreService")
local MoneyStore = ds:GetOrderedDataStore("MoneyStore")
local LevelStore = ds:GetOrderedDataStore("LevelStore")
local ExpStore = ds:GetOrderedDataStore("ExpStore")
local IncomeStore = ds:GetOrderedDataStore("IncomeStore")
local mng1Store = ds:GetOrderedDataStore("mng1Store")
local mng10Store = ds:GetOrderedDataStore("mng10Store")
local mng100Store = ds:GetOrderedDataStore("mng100Store")
local mng1000Store = ds:GetOrderedDataStore("mng1000Store")
local mng10000Store = ds:GetOrderedDataStore("mng10000Store")
local mng100000Store = ds:GetOrderedDataStore("mng100000Store")

game.Players.PlayerAdded:Connect(function(player)
	
	local playerkey = "Player_"..player.UserId
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local money = Instance.new("IntValue", leaderstats)
	money.Name = "Money"
	money.Value = MoneyStore:GetAsync(playerkey) or 0
	MoneyStore:SetAsync(playerkey, money.Value)
	
	local level = Instance.new("IntValue", leaderstats)
	level.Name = "Level"
	level.Value = LevelStore:GetAsync(playerkey) or 1
	LevelStore:SetAsync(playerkey, level.Value)
	
	local expReq = Instance.new("IntValue", leaderstats)
	expReq.Name = "expReq"
	expReq.Value = ExpStore:GetAsync(playerkey) or 100
	ExpStore:SetAsync(playerkey, expReq.Value)

	local autoIncome = Instance.new("IntValue", leaderstats)
	autoIncome.Name = "Income"
	autoIncome.Value = IncomeStore:GetAsync(playerkey) or 0
	IncomeStore:SetAsync(playerkey, autoIncome.Value)
	
	local mng1 = Instance.new("IntValue", leaderstats)
	mng1.Name = "mng1"
	mng1.Value = mng1Store:GetAsync(playerkey) or 0
	mng1Store:SetAsync(playerkey, mng1.Value)
	
	local mng10 = Instance.new("IntValue", leaderstats)
	mng10.Name = "mng10"
	mng10.Value = mng1Store:GetAsync(playerkey) or 0
	mng10Store:SetAsync(playerkey, mng10.Value)
	
	local mng100 = Instance.new("IntValue", leaderstats)
	mng100.Name = "mng100"
	mng100.Value = mng1Store:GetAsync(playerkey) or 0
	mng100Store:SetAsync(playerkey, mng100.Value)
	
	local mng1000 = Instance.new("IntValue", leaderstats)
	mng1000.Name = "mng1000"
	mng1000.Value = mng1Store:GetAsync(playerkey) or 0
	mng1000Store:SetAsync(playerkey, mng1000.Value)
	
	local mng10000 = Instance.new("IntValue", leaderstats)
	mng10000.Name = "mng10000"
	mng10000.Value = mng1Store:GetAsync(playerkey) or 0
	mng10000Store:SetAsync(playerkey, mng10000.Value)
	
	local mng100000 = Instance.new("IntValue", leaderstats)
	mng100000.Name = "mng100000"
	mng100000.Value = mng1Store:GetAsync(playerkey) or 0
	mng100000Store:SetAsync(playerkey, mng100000.Value)	
	
	local function updateStats()
		MoneyStore:SetAsync(playerkey, money.Value)
		LevelStore:SetAsync(playerkey, level.Value)
		ExpStore:SetAsync(playerkey, expReq.Value)
		IncomeStore:SetAsync(playerkey, autoIncome.Value)
		mng1Store:SetAsync(playerkey, mng1.Value)
		mng10Store:SetAsync(playerkey, mng10.Value)
		mng100Store:SetAsync(playerkey, mng100.Value)
		mng1000Store:SetAsync(playerkey, mng1000.Value)
		mng10000Store:SetAsync(playerkey, mng10000.Value)
		mng100000Store:SetAsync(playerkey, mng100000.Value)

		print("Updated")
	end

	money.Changed:Connect(function()
		if money.Value > expReq.Value then
			level.Value = level.Value+1
			expReq.Value = expReq.Value*1.2
			print("money is greater than moneyreq")
		end
	end)
	game.Players.PlayerRemoving:Connect(updateStats)

end)



This is definitely not an efficient way to do it, I would store all of the data in a single store. Then have that be a table. Also, you should separate the parent argument from Instance.new("Type", <parent>), as in remove it.
Also,

game:BindToClose(function() wait(5) end)

So to recap, fix your Instance.new’s, your DataStores, and add the line of code above.

How would I store more than one value in a single datastore?

Try Using Datastore2 How to use DataStore2 - Data Store caching and data loss prevention

2 Likes

I would recommend ProfileService instead, but that’s up for @zDomivs .

1 Like

I wasn’t recommending anything other than typical roblox datastores lol, I was just saying put everything in a table and grab from it.

Hey there!
As @MastersNetwork has already said, this is not an efficient way of saving your data.
I would recommend having one datastore setup and using different data store KEYS to save your data. An Example of this would be as follows

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("name")

game.Players.PlayerRemoving:Connect(function(plr)
	DS:SetAsync(plr.UserId.."-cash", "the value you would like to save here")
end)
3 Likes

Thanks a lot! That’s what I was really looking for when I asked

1 Like

No, still a slow and bad way to store, for peak speed, you’d want:

local DSS = game:GetService("DataStoreService");
local DS = DSS:GetDataStore("PlayerStore");

game.Players.PlayerRemoving:Connect(function(player)
   -- Put everything into a single table:
   -- This takes up less time to retrieve and write (DataStores are async)
   -- Less writes: less possibility for data loss.
   local data = {};
   data.Money = player.leaderstats.Money.Value
   data.Level = player.leaderstats.Level.Value
   data.expReq = player.leaderstats.expReq.Value
   data.autoIncome = player.leaderstats.Income.Value
   -- ...
   DS:SetAsync(player.UserId, data)
end)

Additionally, I haven’t yet noticed, but in the initializing segment:

local money = Instance.new("IntValue", leaderstats)
money.Name = "Money"
money.Value = MoneyStore:GetAsync(playerkey) or 0
MoneyStore:SetAsync(playerkey, money.Value)
-- This line is troubling. It shouldn't be there.
-- My logic is that, you've just now retrieved the data from that key, re-writing is only going to leave place for more issues, and re-writing would put the data back, exactly the same as before.
-- i.e: money.Value == dataStore[playerkey] until session exit. So remove all of those.
MoneyStore:SetAsync(playerkey, money.Value)

That’s true, but again, for beginners, loading data would be a nightmare for them.