- What do you want to achieve? Keep it simple and clear!
I am trying to make like a currency system and the new player will be given 1000 and a datastore that saves all the remaining cash if they bought something from the store.
- What is the issue? Include screenshots / videos if possible!
the datastore does save but when I rejoin I still got 1000 even though I have already set the Value to 0 in the script.
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried researching it on google and developer forum but I could not find a way through to fix this…
local VND = "VND"
local DataStore = game:GetService("DataStoreService"):GetDataStore("VND")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local currency = Instance.new("IntValue")
currency.Name = VND
currency.Parent = folder
currency.Value = 0
local ID = VND..("-")..player.UserId
print(ID)
local data = DataStore:GetAsync(ID)
if data then
currency.Value = data
print("data loaded")
else
currency.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall (function()
local ID = VND..("-")..player.UserId
DataStore:SetAsync(ID, player.leaderstats.VND.value)
end)
if not success then
warn('could not save data')
end
end)
I really hope someone could help me out
Thanks!