I have it so that when a player leaves it creates a table with their data, and when they join it retrieves that data, but if they have no data it sets a default value. But it doesn't seem to save the data because when you join, it just sets it to the default value no matter what
script:
local Players = game:GetService("Players") local DataStoreService = game:GetService("DataStoreService") local PlayerData = DataStoreService:GetDataStore("TestDataV1") Players.PlayerAdded:Connect(function(player) local Data; local DataFetchSuccess,ErrorMessage = pcall(function() Data = PlayerData:GetAsync(player.UserId) end) if not DataFetchSuccess then player:Kick("Unable to load your data, please rejoin.") return end local Stats = Instance.new("Folder") Stats.Name = "Stats" local Cash = Instance.new("IntValue", Stats) Cash.Name = "Cash" local Stars = Instance.new("IntValue", Stats) Stars.Name = "Stars" Stats.Parent = player if Data then Cash.Value = Data.Cash Stars.Value = Data.Stars else Cash.Value = 100 Stars.Value = 5 end end) Players.PlayerRemoving:Connect(function(player) local Data = { } Data.Cash = player.Stats.Cash.Value Data.Stars = player.Stats.Stars.Value local DataWriteSuccess,ErrorMessage = pcall(function() PlayerData:SetAsync(player.UserId, Data) end) if not DataWriteSuccess then warn("Couldn't save data of "..player.Name) end end)
You need to use game:BindToClose
alongside PlayerRemoving. The problem with only having PlayerRemoving is that the server already shuts down whilst your data is still in the middle of saving. BindToClose will prevent this because the function must have run before the game server shuts down.
You forgot to use DataStore:SetAsync
if the player is new, so there is no data made. Instead, you should try this:
local Players = game:GetService("Players") local DataStoreService = game:GetService("DataStoreService") local PlayerData = DataStoreService:GetDataStore("TestDataV1") Players.PlayerAdded:Connect(function(player) local Data; local DataFetchSuccess,ErrorMessage = pcall(function() Data = PlayerData:GetAsync(player.UserId) end) if not DataFetchSuccess then player:Kick("Unable to load your data, please rejoin.") return end local Stats = Instance.new("Folder") Stats.Name = "leaderstats" local Cash = Instance.new("IntValue", Stats) Cash.Name = "Cash" local Stars = Instance.new("IntValue", Stats) Stars.Name = "Stars" Stats.Parent = player if Data then Cash.Value = Data.Cash Stars.Value = Data.Stars else -- Data doesn't exist local DefaultValues = { cash = 100, stars = 5 } PlayerData:SetAsync(player.UserId, DefaultValues) -- set data store to default values Cash.Value = DefaultValues.cash -- set leaderstats to default values Stars.Value = DefaultValues.stars end end) Players.PlayerRemoving:Connect(function(player) local Data = { } Data.Cash = player.leaderstats.Cash.Value Data.Stars = player.leaderstats.Stars.Value local DataWriteSuccess,ErrorMessage = pcall(function() PlayerData:SetAsync(player.UserId, Data) end) if not DataWriteSuccess then warn("Couldn't save data of "..player.Name) end end)
I highly recommend the "Datastore Editor" plugin by Crazyman32, it really helped me do this.