Hello, I am creating a datastore and I have a problem where when saving the contents in the “PlayerStats” folder, it doesn’t save any new data.
I can’t seem to figure it out.
example: I start the game with “LV” 1 and change the NumverValue to 9, I leave the game and it prints that it saved it as 1 still.
local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(player)
local PlayerStatsKey = "PlayerStats-".. player.UserId
local StatsTemplate = script.Stats
local PlayerStatsFolder = Instance.new("Folder", player)
PlayerStatsFolder.Name = "PlayerStats"
local PlayerStatsSave = PlayerDataStore:GetAsync(PlayerStatsKey)
for _, Template in pairs(StatsTemplate:GetChildren()) do
local NewFolderStats = Instance.new(Template.ClassName, PlayerStatsFolder)
NewFolderStats.Name = Template.Name
NewFolderStats.Value = Template.Value
end
if PlayerStatsSave then
for Value, Stat in pairs(PlayerStatsFolder:GetChildren()) do
Stat.Value = PlayerStatsSave[Value]
print("Loaded ".. player.Name .." ".. Stat.Name .." Value of "..Stat.Value)
end
else
end
print("Loaded")
end)
game.Players.PlayerRemoving:Connect(function(player)
local PlayerStatsKey = "PlayerStats-".. player.UserId
local PlayerStatsFolder = player.PlayerStats
print(PlayerStatsFolder.LV.Value)
local PlayerStatsTable = {}
for _, Stat in pairs(PlayerStatsFolder:GetChildren()) do
table.insert(PlayerStatsTable, Stat.Value)
print("Saved ".. player.Name .." ".. Stat.Name .." Value of "..Stat.Value)
end
PlayerDataStore:SetAsync(PlayerStatsKey, PlayerStatsTable)
print("Saved")
end)