Fixing the DataStore

Hello everyone! I have a problem with DataStore2. I am not sure why but when a player joins the game, and they did not get any gold or buy anything from the shop, most of the time the value of their gold is 0. But after they buy something or earn gold it returns to the original amount. So the amount is stored, but when they first join most of the time it is 0. But when you look at other people’s gold it shows their correct values. This is only for the player’s own gold. What is the problem? Here is the Script:

local DataStore2 = require(1936396537)

DataStore2.Combine("MasterKey","Gold")

game.Players.PlayerAdded:Connect(function(plr)
	local dataGold = DataStore2("Gold", plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"
	
	local gold = Instance.new("IntValue", folder)
	gold.Name = "Gold"
	
	if dataGold:Get() ~= nil then
		gold.Value = dataGold:Get()
	else
		gold.Value = 100
	end
	
	gold.Changed:Connect(function()
		dataGold:Set(gold.Value)
	end)
	
end)

I don’t recommend that you use changed function because the datastore script has limits, take look at this.

this is a better script for the leaderstats datastore :

local DataStore = game:GetService("DataStoreService")
local myDataStore = DataStore:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"
	
	local gold = Instance.new("IntValue", folder)
	gold.Name = "Gold"
	gold.Value = 0

        local data
	local success, errormessage = pcall(function()
	data = myDataStore:GetASync(plr.UserId.."-Coins")
	end)

        if success then
		Coins.Value = data
	else
		print("There was an error while giving Player Data.")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormessage = pcall(function()
	
	myDataStore:SetASync(plr.UserId.."-Coins", plr.leaderstats.Coins.Value)
	end)

	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error while saving Player Data.")
		warn(errormessage)
	end
end)

This is normal Datastore he is using datastore2

And the problem is you are setting the value of gold not the value in the datastore I know you setting the value when the gold value changed but I wouldn’t recpmmend that I think datastore2 has an in-built changed I dont remember might have to see the documentation.

1 Like

Okay the problem is I have asked everyone already, but no one has been able to really answer it.

You can shorten this to

gold.Value = dataGold:Get(0)

the ‘0’ is a parameter in the :Get method, basically if the value is nil, the default value is 0, if the value isn’t nil, it’ll return the last saved value.

I’m not sure if this would fix the issue but I am just helping you clean up your code.

1 Like

Yeah that might actually fix it

Still not working.


After earning more gold it shows.

This is the whole script now.

	local DataStore2 = require(1936396537)

DataStore2.Combine("MasterKey","Gold")

game.Players.PlayerAdded:Connect(function(plr)
	local dataGold = DataStore2("Gold", plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"

	local gold = Instance.new("IntValue", folder)
	gold.Name = "Gold"

	gold.Value = dataGold:Get(0)

	gold.Changed:Connect(function()
		dataGold:Set(gold.Value)
	end)

end)

I assume API Services are activated?

Yes of course I am not sure why let me test on my test map to see if it does the same. If it works on my test map, then that means its a problem for my game.

Also, if you want to update your gold value, you can use the datastore2 object directly such as

dataGold:Increment(value)

This way you can directly change this stat in other scripts.

Yep I know what is wrong now. I tested this in my Testing Game, and it works fine four times I rejoined. Killing Zombies give you gold and the coins too so the only way is the killing script I added in Starter Player Script would that be it? I will put that in my Testing Game to make sure and see. But it works the same so what would cause this problem?

image In the output it says HTTP is forbidden?

Are you using multiple datastores?

Yes with the inventory save, but before it did this already.

Also I added a boolValue inside of ServerStorage for SaveInStudio and checkmarked it, and it works in Studio now

You should only be using one datastore, the Datastore2 Combine method allows you to combine multiple keys into one big dictionary which is ultimately saved into one datastore. This way you aren’t wasting calls to the server as datastore2 makes multiple calls to ensure no data loss.

I do not think that is the problem. When I first started using DataStore2 for the Gold leaderstat, it was like this.

Instead of using gold.Changed, don’t you want to use

dataGold:OnUpdate(function(newValue)
--do code
end)

This way it updates when the Datastore updates, not when the Gold Folder does.

Edit: You can also update the Gold leaderstat in that function since it gives you the new value.

Okay, let me try that right now.