-- Custom Services

local database = require(game:GetService("ServerScriptService").Database.DatabaseHandler)

local function OnCharacterAdded(character)
	
	--Setup variables
	local statsFolder = Instance.new("Folder")
	statsFolder.Name = "Stats"
	
	local safeZoneBool = Instance.new("IntValue")
	safeZoneBool.Name = "isSafe"
	safeZoneBool.Parent = statsFolder
	
	statsFolder.Parent = character
end

players.PlayerAdded:Connect(function(player)
	local playerID = player.UserId
	
	
	local playerProfile = database.GetProfile(playerID)
	if not playerProfile or playerProfile == 0 then
		database.CreateProfile(playerID)
	else
		-- Perform this check in case someone adds new properties to the data template
		database.UpdateProfileTemplate(playerID)
	end
	
	player.CharacterAdded:Connect(OnCharacterAdded)
	player:LoadCharacter()
end)
	
players.PlayerRemoving:Connect(function(player) 
	local playerID = player.UserId
	database.SaveProfile(playerID)
end)
