I'm currently using DataStore to store two things: Inventory and wins/coin counting.
When it comes to the wins and the coins, it saves fine, but when it comes to saving a few other variables I'm using for an Inventory, it doesn't work so well.
It works in game, but once I leave the data resets. Help?
Joining game/loading data
local DataStore = game:GetService("DataStoreService"):GetDataStore("WarGamesSaver")
game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("Model", player)
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue", leaderstats)
cash.Name = "Coins"
local wins = Instance.new("IntValue", leaderstats)
wins.Name = "Wins"
local InvoSlot1 = Instance.new("StringValue", player)
InvoSlot1.Name = "Slot1"
InvoSlot1.Value = "Default"
local InvoSlot2 = Instance.new("StringValue", player)
InvoSlot2.Name = "Slot2"
InvoSlot2.Value = "Empty"
local InvoSlot3 = Instance.new("StringValue", player)
InvoSlot3.Name = "Slot3"
InvoSlot3.Value = "Empty"
local InvoSlot4 = Instance.new("StringValue", player)
InvoSlot4.Name = "Slot4"
InvoSlot4.Value = "Empty"
local Equiped = Instance.new("StringValue", player)
Equiped.Name = "Equiped"
Equiped.Value = "Default"
local key = "player-"..player.userId
local savedValues = DataStore:GetAsync(key)
if savedValues then
cash.Value = savedValues[1]
wins.Value = savedValues[2]
else
local valuesToSave = {cash.Value, wins.Value, InvoSlot1.Value, InvoSlot2.Value, InvoSlot3.Value, InvoSlot4.Value, Equiped.Value, }
DataStore:SetAsync(key, valuesToSave)
end
end)
Leaving game/saving data
local DataStore = game:GetService("DataStoreService"):GetDataStore("WarGamesSaver")
game.Players.PlayerRemoving:connect(function(player)
local key = "player-"..player.userId
local valuesToSave = {player.leaderstats.Coins.Value, player.leaderstats.Wins.Value, player.Slot1.Value, player.Slot2.Value, player.Slot3.Value, player.Slot4.Value, player.Equiped.Value}
DataStore:SetAsync(key, valuesToSave)
end)
It has trouble saving saving everything except for "coins" and "wins"

1 comment
I am having problems with saving items too. This could help me out aswell.