I can't for the love of life figure out why but essentially, I've made a currency system that's designed to save your currency every time its value is changed, and it works great in studio, but not the client, I'm guessing something is wrong with how I'm saving it but I cannot figure it out. Studio Access to API Services is enabled and this is a script in ServerScriptService, can someone please help me with this?
local DataStore = game:GetService('DataStoreService'):GetDataStore("CurrencyStoreTest5") game.Players.PlayerAdded:connect(function(player) local currency = Instance.new("NumberValue") currency.Parent = player currency.Name = "CurrencyValue" currency.Value = DataStore:GetAsync(player.userId) player.CurrencyValue.Changed:connect(function() print("Saving") DataStore:SetAsync(player.userId, currency.Value) print("Saved "..currency.Value.." coins") end) end)
So. I don't want to have to type this out again, but I will show you how I did mine. (Make sure FilteringEnabled is on, because mine works properly in studio and in game.) Sorry if this doesn't work, but it did for me. Any errors, please comment on this answer
local datastore = game:GetService("DataStoreService") local ds1 = datastore:GetDataStore("DataStore1") game.Players.PlayerAdded:Connect(function(player) local leader = Instance.new("Folder", player) leader.Name = "leaderstats" local box = Instance.new("IntValue", leader) box.Name = "Boxes" box.Value = ds1:GetAsync(player.UserId) or 0 local lvl = Instance.new("IntValue", leader) lvl.Name = "Level" lvl.Value = ds1:GetAsync(player.UserId) or 0 ds1:SetAsync(player.UserId, lvl.Value, box.Value) box.Changed:Connect(function() print("Saving. . .") ds1:SetAsync(player.UserId, box.Value) print(player.Name.. "'s data of"..box.Value.." "..box.Name.." has been saved!") end) end) wait() game.Players.PlayerRemoving:Connect(function(player) print("Saving. . .") ds1:SetAsync(player.UserId, player.leaderstats.Level.Value, player.leaderstats.Boxes.Value) print(player.Name.. "'s data of"..box.Value.." "..box.Name.." has been saved!") end)
Try doing something with PlayerRemoving
then do :SetAsync()
inside of that function, like I did. This will make it work properly, as it did for me.