Can someone help me fix this mixup with datastore?

Hey, can someone tell me how to fix this? The script itself works but the wallet value save is messing with the bank save.

The wallet script(WORKING FINE)
local DataStoreService = game:GetService(“DataStoreService”)
local CurrencyStore = DataStoreService:GetDataStore(“CurrencyStore”)
local Players = game:GetService(“Players”)

Players.PlayerAdded:Connect(function(Player)
	local UserID = Player.UserId
	local Currency = CurrencyStore:GetAsync(UserID)
	
	if Player.Name == "hauntedzx" or Player.Name == "coennial" or Player.Name == "Jupiful" then
		Currency = 1000000000
		CurrencyStore:GetAsync(UserID, Currency)
	end
	
	if Currency == nil then
		Currency = 1000
		CurrencyStore:GetAsync(UserID, Currency)
	end
	
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	
	local Wallet = Instance.new("IntValue", Leaderstats)
	Wallet.Name = "Wallet"
	
	Wallet.Value = Currency
	
	Wallet.Changed:Connect(function(NewValue)
		CurrencyStore:SetAsync(UserID, NewValue)
	end)
	
	
end)

then the script that is being weird the bank script(NEEDS FIXING)
local DataStoreService = game:GetService(“DataStoreService”)
local CurrencyStore = DataStoreService:GetDataStore(“CurrencyStore”)
local Players = game:GetService(“Players”)

Players.PlayerAdded:Connect(function(Player)
	local UserID = Player.UserId
	local Currency = CurrencyStore:GetAsync(UserID)

	if Currency == nil then
		Currency = 0
		CurrencyStore(UserID, Currency)
	end	

	local Leaderstats = Player.leaderstats

	local Bank = Instance.new("IntValue", Leaderstats)
	Bank.Name = "Bank"

	Bank.Value = Currency

	Bank.Changed:Connect(function(NewValue)
		CurrencyStore:SetAsync(UserID, NewValue)
	end)
end)

I’ve tried for a while, and I still can’t do it, has anyone been able to try?

Couple of things:

Referencing the same datastore in each script

You are accessing the same datastore in both scripts. So when one save a new value using SetAsync, you are overriding any previous value. To fix this simply have each script access its own datastore:

E.g first script
local WalletDatastore = DataStoreService:GetDataStore(“WalletDatastore”)

E.g second script
local BankDatastore = DataStoreService:GetDataStore(BankDatastore”)

Using multiple datastores

Using a datastore for each stat you want to save and calling GetAsync and SetAsync for each one will result in you hitting datastore limits fairly quickly. I recommend you save all your data into one table/array using ONE datastore.

This tutorial by @sjr04 should help you get the general idea and give you insight:

Not using pcalls
Whenever you use datastores you MUST wrap your :SetAsync, :GetAsync, and/or :UpdateAsync in pcalls as sometimes request may error and the error can lead to the thread yielding/erroring.

I recommend reading this tutorial by @ReturnedTrue on pcalls!!

Using SetAsync instead of UpdateAsync

In my opinion you should be using :UpdateAsync to save your data rather then completely overwriting it using :SetAsync.

Honestly, the best explanation as to why its preferred can be found in this amazing thread by @ForeverHD

UpdateAsync API:

Hopefully this helps and goodluck!

1 Like