Here's the script:
local DataBaseStore = game:GetService("DataStoreService"):GetDataStore("AllTheStatsAreHere")
local Statsholder = script.Stats
game.Players.PlayerAdded:Connect(function(NewPlayer)
local key = "Player-ID:" .. NewPlayer.UserId
local GetSave = DataBaseStore:GetAsync(key)
local DataBaseFolder = Instance.new("Folder", NewPlayer)
DataBaseFolder.Name = "PlayerStats"
for i, Stats in pairs(Statsholder:GetChildren()) do
local NewStats = Instance.new(Stats.ClassName)
NewStats.Name = Stats.Name
NewStats.Value = Stats.Value
NewStats.Parent = DataBaseFolder
end
if GetSave then
for i, Stats in pairs(script.Stats:GetChildren()) do
DataBaseFolder[Stats.Name].Value = GetSave[i]
print(i, Stats.Name, Stats.Value)
end
else
local StatsToSave = {}
for i, Stats in pairs(DataBaseFolder:GetChildren()) do
table.insert(StatsToSave, Stats.Value)
print(i, Stats.Name, Stats.Value)
end
DataBaseStore:SetAsync(key, StatsToSave)
end
while wait(15) do
local StatsToSave = {}
for i, Stats in pairs(DataBaseFolder:GetChildren()) do
table.insert(StatsToSave, Stats.Value)
print(i, Stats.Name, Stats.Value)
end
DataBaseStore:SetAsync(key, StatsToSave)
end
game.Players.PlayerRemoving:Connect(function(OldPlayer)
local key = "Player-ID:" .. OldPlayer.UserId
local StatsToSave = {}
for i, Stats in pairs(DataBaseFolder:GetChildren()) do
table.insert(StatsToSave, Stats.Value)
print(i, Stats.Name, Stats.Value)
end
DataBaseStore:SetAsync(key, StatsToSave)
end)
end)
It should technically save the data when the player is leaving the game yet it doesn't, i cannot figure out why, hope someone can help, thanks in advance.
