Hello, I am trying to make a money datastore, but for some reason it doesnt work.
Script:
local DSS = game:GetService("DataStoreService")
local coinSave = DSS:GetDataStore("CoinSave")
game.Players.PlayerAdded:Connect(function(plr)
local cash = plr:WaitForChild("leaderstats"):WaitForChild("Credits")
local data
local success, err = pcall(function()
data = coinSave:GetAsync(plr.UserId)
end)
if success then
cash.Value = data
print("Coin data loaded")
else
print("Data not loaded")
end
end)
local function saveData(plr)
local cash = plr:WaitForChild("leaderstats"):WaitForChild("Credits")
local data
local success, err = pcall(function()
data = coinSave:SetAsync(plr.UserId, cash.Value)
end)
if success then
print("Coin Data saved")
else
print("Coin Data not saved")
end
end
game.Players.PlayerRemoving:Connect(function(plr)
saveData(plr)
end)
game:BindToClose(function()
for i, v in pairs(game.Players:GetPlayers()) do
saveData(v)
end
end)
According to the prints everything goes correctly