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)

1 comment
I have the same problem as well. I don't know what's happening.