So today I read up on Data Store and watched some videos on Youtube. I made a script using PeasFactory's tutorial on Youtube and I am wondering why it isn't working for my game? I have altered the values to suit my needs, but I think in doing so I messed it up and I cannot find what is wrong. I want the script to save 'SpiritCoins' (being the currency of my game) and 'Survivals' (the number of times people survive my disasters/minigames).
local DataStore = game:GetService("DataStoreService"):GetDataStore("SpiritCoinsAndSurvivals") game.Players.PlayerAdded:connect(function(player) local stats = Instance.new("IntValue", player) stats.Name = "leaderstats" local coins = Instance.new("IntValue", player) coins.Name = "SpiritCoins" local survivals = Instance.new("IntValue", player) survivals.Name = "Survivals" local key = "player-"..player.userId local savedValues = DataStore:GetAsync(key) if savedValues then --Save format: {coins, survivals} coins.Value = savedValues[1] survivals.Value = savedValues[2] else local valuesToSave = {coins.Value, survivals.Value} DataStore:SetAsync(key, valuesToSave) end end)
And here is the script for when the player leaves the game so it saves the data.
local DataStore = game:GetService("DataStoreService"):GetDataStore("SpiritCoinsAndSurvivals") game.Players.PlayerRemoving:connect(function(player) local key = "player-"..player.userId --Save key: {coins, survivals} local valuesToSave = {player.leaderstats.SpiritCoins.Value, player.leaderstats.Survivals.Value} DataStore:SetAsync(key, valuesToSave) end)
Though I have read on the wiki an UpdateAsync is a good thing to have. I will need to learn about that and add it to the leaving script. If there is anything I left out, please don't hesitate and keep in mind I am still a beginner level scripter.