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)
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.
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.
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.
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?
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.