@Maelstorm_1973, this is what the script looks like currently:
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
--other leaderstats go here
local upgrades = Instance.new("NumberValue")
upgrades.Name = "Multiplier"
upgrades.Parent = player
local playerUserId = player.UserId.."_Player"
local data = playerData:GetAsync(playerUserId)
if data then
--other leaderstats go here
upgrades.Value = data['Multiplier']
if upgrades.Value <= 0 then
upgrades.Value = 1
end
else
--other leaderstats go here
upgrades.Value = 1
end
end
local function create_table(player)
local player_stats = {
leaderboard = {};
data = {};
}
for _, stat in pairs(player:GetChildren()) do
if stat:IsA("NumberValue") then
player_stats.data[stat.Name] = stat.Value
end
end
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats.leaderboard[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player)
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = player.UserId.."_Player"
playerData:SetAsync(playerUserId, player_stats)
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
game:BindToClose(function()
for i, player in pairs(game.Players:GetChildren()) do
onPlayerExit(player)
end
end)
Not sure where I should add the createDataValues()
code.
If you need the other stats I can add them in.