This is the problem i’ve been getting a lot. The reason is that in test mode. You’re the only person there. Which means if you left the server will just closed which makes the saving closed as well. To prevent this you’ll need to use the Bind to close function. You can get more information at the link i’ve given you.
The first argument of SetAsync is the key on which you are saving the data. playerData is the datastore you are saving it to, so you cant use it as a key. You should use player.UserId as a key.
It should definetly work now, maybe the problem now is that server closes before it could save the data like @bookgamery555gta said, in that case bindtoclose should help.
Hello, the problem here is most likely because you didn’t load your player’s data when they joined the game; this triggers an error whenever you want to save the player’s data when they join because there was no data that was loaded in the first place.
The best way to go about this is to use this script I’ve made, this is probably one of the most basic uses of loading and saving player data:
local DataStoreService = game:GetService("DataStoreService")
local CashDataStore = DataStoreService:GetDataStore("CashDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder",Player)
leaderstats.Name = "leaderstats" -- you can only name this 'leaderstats'.
local Cash = Instance.new("Intvalue",leaderstats)
Cash.Name = "Cash"
local UserId = Player.UserId
local Data = nil
local s, e = pcall(function()
Data = CashDataStore:GetAsync(UserId) -- loads data
end
if Data ~= then -- if the player's data is not nil (meaning this isnt their first time playing) then it will load the player's cash data.
Cash.Value = Data
print("Loaded Data")
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local UserId = Player.UserId
local Data = Player.leaderstats.Cash.Value
local s, e = pcall(function()
CashDataStore:SetAsync(UserId, Data) -- saves data to the player thats leaving
end
if s then
print("Saved Data")
else
warn("Error saving data")
end
end)