So, I’m trying to make a leaderstats data store script and for some reason, the stats don’t save and I’ve used this script multiple times before. And I’m getting no errors in the output and I had API services on and even tested it in the actual game.
local DataStoreService = game:GetService("DataStoreService")
local DataStoreName = DataStoreService:GetDataStore("playerDataStore")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local levels = Instance.new("IntValue")
levels.Name = "Levels"
levels.Parent = stats
local data
local success, errormessage = pcall(function()
data = DataStoreName:GetAsync(player.UserId.. "-Levels")
levels.Value = data
end)
if success then
print("Data Loaded")
else
print("Error")
wait(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStoreName:UpdateAsync(player.UserId.."-Levels",function(old)
local newvalue1 = old
newvalue1 = player.leaderstats.Levels.Value
end)
end)
if success then
print("Data has been saved!")
else
print("Error")
wait(errormessage)
end
end)
I don’t think you meant to wait() the error message…
Anyway, according to the documentations, when using an UpdateAsync function, said function is supposed to return the new value. Add a return function, let us know how it goes.
I added a return function for the new value and it still doesn’t save, and there are no error messages. And yes, I changed wait(errormessage) into warn(errormessage)
Are you playtesting this in studio? If so, please verify that you enabled Datastores and other APIs in the settings. This would be located in Game Settings, in the security tab.
FIRST ISSUE
Your update async function doesn’t get a value because you did not return any values. You need to add this to the line after you set the value of newvalue1.
return newvalue1
SECOND ISSUE
Add a bind to close so that if the last player leaves the server does not shut down without firing its last functions. This is not required.