here is my script for the data store
local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("myDataStore") game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local cash = Instance.new("IntValue") cash.Name = "Cash" cash.Parent = leaderstats local data local success, errormessage = pcall(function() data = myDataStore:GetAsync(player.UserId.."-cash") end) if success then cash.Value = data else print("There was a error whilist getting your data") warn(errormessage) end end) game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() myDataStore:GetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value) end) if success then print("Player Data sccessfully saved!") else print("There was a error when saving your data") warn(errormessage) end end)
game:BindToClose
to make sure that the server stays open after all players leave long enough to save their data.Alternatively, if you use something like ProfileService, it takes a bit of getting used to, but it handles all that in addition to auto-saving, session locking, and more.