Hi guys. So I've been checking some wiki's concerning the DataStore, and I've ended up with an extremely simple DataStoreHandler which is supposed to save the amount of gold a user has upon leaving/entering, and then loading his latest amount when joining back.
Now, I have two problems;
- When spending the gold on the in-game store, it reduces your gold. (Example: from 300 to 200 after purchasing a 100$ item)
Yet whenever the next payday comes, it takes your amount from BEFORE it was reduced, so 300 in this case, and adds the pay to that. Making your gold 310, rather than 210. It seems to be taking the old DataStore and keeps loading that, rather than the newest one.
So in the end, when the player does get his paycheck and leaves, he does keep the new amount. It seems to only affect a reduced amount!
Here is my simple Handler:
local ds = game:GetService('DataStoreService'):GetDataStore('Gold')
game.Players.PlayerAdded:Connect(function(plr)
local key = 'id-'..plr.userId
local gold = plr:WaitForChild('Gold') --IntValue inside the Player
local GetSaved = ds:GetAsync(key)
if GetSaved then
gold.Value = GetSaved[1]
else
local savenmr = {gold.Value}
ds:SetAsync(key, savenmr)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local key = 'id-'..plr.userId
local table = {plr.Gold.Value}
ds:SetAsync(key, table)
end)
and in-case they might collide, here is my Paycheck handler, on a seperate script:
game.Players.PlayerAdded:Connect(function(plr)
if plr:FindFirstChild("Gold") then
else
local gold = Instance.new("IntValue")
gold.Parent = plr
gold.Name = "Gold"
end
end)
game.Players.PlayerAdded:Connect(function(player)
while true do
wait(60)
local gold = player.Gold
gold.Value = gold.Value + 10
end
end)
Any help regarding this problem is more than welcome!

1 comment
You're connecting multiple PlayerAdded events, line 10 of your second code will run over line 1's code.