Can somebody help me with a datastore script? I just recently started trying to learn it and it isn't working, API services is on and it prints "Data was loaded!", except it doesn't save when you leave for some reason, I don't know why it isn't saving
Here's the script:
local DSS = game:GetService("DataStoreService")
local Datastore = DSS:GetDataStore("Datastore-1")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue", leaderstats)
cash.Name = "Cash"
local data
local success, errormessage = pcall(function()
data = Datastore:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
print("Data was loaded!")
else
print("Error when getting data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
Datastore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash)
end)
if success then
print("Data successfully saved!")
else
print("Data was not saved!")
wait(errormessage)
end
end)
Did I do anything wrong?

1 comment
ive got the same problem