Datastore Problems

Hello Developers,

I am a builder not a programmer therefore I do not have much expertise in this topic, but I require some help because I was trying to create an economy script and I am left without script errors; I have only one of the 2 data stores saving the values within the player. This may be easy to fix I’m not entirely sure, and to note I do have API access enabled and as I said one data store is currently working.

Here is the script, if someone could figure this out for me that would be amazing.

local DATA_STORE_SERVICE = game:GetService("DataStoreService")
local MONEY_STORE = DATA_STORE_SERVICE:GetDataStore("EconomyMoney")
local JOIN_STORE = DATA_STORE_SERVICE:GetDataStore("JoindBefore")
local GAME_PLAYERS = game.Players


GAME_PLAYERS.PlayerAdded:Connect(function(PLAYER)
	local MONEY_STAT = Instance.new("NumberValue")
	MONEY_STAT.Name = "MONEY_AMOUNT"
	MONEY_STAT.Parent = PLAYER
	
	local JOINED_STATUS = Instance.new("BoolValue")
	JOINED_STATUS.Name = "JOINED_BEFORE"
	JOINED_STATUS.Parent = PLAYER
	
	local DATA = MONEY_STORE:GetAsync(PLAYER.UserId)
	
	if JOINED_STATUS.Value == false then
		MONEY_STAT.Value = MONEY_STAT.Value + 50
		JOINED_STATUS.Value = true
		JOIN_STORE:SetAsync(PLAYER.UserId, JOINED_STATUS.Value)
	end
	
	while wait(5) do
		MONEY_STAT.Value = MONEY_STAT.Value + 50
	end
	
	MONEY_STAT.Changed:Connect(function(NEW_VALUE)
   	 	MONEY_STORE:SetAsync(PLAYER.UserId, MONEY_STAT.Value)
	end)
end)

GAME_PLAYERS.PlayerRemoving:Connect(function(PLAYER)
	local MONEY_STAT = PLAYER.MONEY_AMOUNT
	MONEY_STORE:SetAsync(PLAYER.UserId, MONEY_STAT.Value)
end)

The JOIN_STORE datastore is working and saves a bool value for when the player has joined before but the other datastore is currently not working.

1 Like

It is bad practice to save with datastores without having a key. I suggest using PLAYER.UserId instead of just PLAYER.

1 Like

My bad, I posted an old version of the script; my friend gave my feedback already to use PLAYER.UserId and after doing that change the script still was not saving the money to the money datastore.

1 Like

On the changed function, there is nothing describing what the “NEW_VALUE” is. So try deleting that and see if it works.

1 Like

Could you further explain in what way it is not working?

Also, I might be missing something something, but it appears that you never originally set MONEY_STAT.Value, to the money value from the DataStore.

You do get the money data here:

local DATA = MONEY_STORE:GetAsync(PLAYER.UserId)

, but I don’t ever see you apply it to the MONEY_STAT NumberValue.

You appear to just be originally setting the MONEY_STAT value to itself, which is 0 by default.

if JOINED_STATUS.Value == false then
		MONEY_STAT.Value = MONEY_STAT.Value + 50 -- you set it to itself plus 50,  but don't incorporate the DATA you just got.
		JOINED_STATUS.Value = true 
		JOIN_STORE:SetAsync(PLAYER.UserId, JOINED_STATUS.Value)
	end
1 Like

Avoid using SetAsync every time MONEY_STAT is changed. It can overload the datastores. Only save every 60 seconds and save when the player leaves the game. Also if you are testing this in studio it may not work. If you are testing in studio, use the BindToClose function.

Try using an IntValue instead of this.
Also you should give a Value to MONEY_STAT before adding anything to it.