I've been trying use UpdateAsync to save data in my game when you leave and this is what I came up with:
local Players = game:GetService("Players") local DataStoreService = game:GetService("DataStoreService") local StoredLevel = DataStoreService:GetDataStore("Level") local StoredExp = DataStoreService:GetDataStore("Exp") local StoredMaxExp = DataStoreService:GetDataStore("MaxExp") Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() StoredLevel:UpdateAsync(player.UserId.."-Level", player.Stats.Level.Value) StoredExp:UpdateAsync(player.UserId.."-Exp", player.Stats.Exp.Value) StoredMaxExp:UpdateAsync(player.UserId.."-MaxExp", player.Stats.MaxExp.Value) end) if success then print("Player Data Successfully Saved!") else warn("Error:"..errormessage) end end)
but instead of saving, I get this message:
16:38:53.316 - Error:Unable to cast value to function
UpdateAsync (<Psst. That's a link!) works by calling a function to update the key you provide.
Here is the code from that wiki page:
local pointsDataStore = game:GetService("DataStoreService"):GetDataStore("Points") game.Players.PlayerAdded:Connect(function(player) local playerKey = "Player_" .. player.UserId -- Give 50 points to players each time they visit local success, err = pcall(function() pointsDataStore:UpdateAsync(playerKey, function(oldValue) --Notice how the in UpdateAsync you pass in the key, and then a function? local newValue = oldValue or 0 newValue = newValue + 50 return newValue end) end) end)
In your code you're just trying to pass in the value, like you would with SetAsync. UpdateAsync is good for if you need to use the old value, or if you may be updating the same key in multiple places.