I was making a data store and when I left the game, it was supposed to print "Data saved" but it didn't, nor did I get an error.
What did I do wrong?
local DataStoreService = game:GetService("DataStoreService")
local fcgDataStore = DataStoreService:GetDataStore("fcgDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
--Currency
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Parent = leaderstats
--Inventory
local inventory = Instance.new("Folder")
inventory.Name = "Inventory"
inventory.Parent = player
--Data
local userId = "Player_"..player.UserId
--Load data
local data
local success, errormessage = pcall(function()
data = fcgDataStore:GetAsync(userId)
end)
if success then
coins.Value = data
end
end)
--Save data
game.Players.PlayerRemoving:Connect(function(player)
local userId = "Player_"..player.UserId
local data = player.leaderstats.Coins.Value
local success, errormessage = pcall(function()
fcgDataStore:SetAsync(userId, data)
end)
if success then
print("Data saved")
else
print("Error saving data")
warn(errormessage)
end
end)

2 comments
How can I add more leaderstats and still make it save?
I edited it and said where and how and an example