-----------------
--| Functions |--
-----------------


local function MakeSkeleton()
	Skeleton = InsertService:LoadAsset(SKELETON_ASSET_ID):GetChildren()[1]
	if Skeleton then
		local head = Skeleton:FindFirstChild('Head')
		if head then
			head.Transparency = 1
		end

		local skeletonScriptClone = SkeletonScript:Clone()
		skeletonScriptClone.Parent = Skeleton
		skeletonScriptClone.Disabled = false

		local creatorTag = Instance.new('ObjectValue')
		creatorTag.Name = 'creator' --NOTE: Must be called 'creator' for website stats
		creatorTag.Value = MyPlayer
		local iconTag = Instance.new('StringValue', creatorTag)
		iconTag.Name = 'icon'
		iconTag.Value = Tool.TextureId
		creatorTag.Parent = Skeleton
	end
end

local function SpawnSkeleton(spawnPosition)
	if Skeleton then
		-- Hellfire
		local firePart = Instance.new('Part')
		firePart.Name = 'Effect'
		firePart.Transparency = 1
		firePart.FormFactor = Enum.FormFactor.Custom
		firePart.Size = Vector3.new()
		firePart.Anchored = true
		firePart.CanCollide = false
		firePart.CFrame = CFrame.new(spawnPosition - Vector3.new(0, 4, 0))
		local fireClone = Fire:Clone()
		fireClone.Parent = firePart
		Delay(0.5, function()
			if fireClone then
				fireClone.Enabled = false
			end
		end)
		DebrisService:AddItem(firePart, 3)
		firePart.Parent = Workspace

		-- Spawn
		local skeletonClone = Skeleton:Clone()
		DebrisService:AddItem(skeletonClone, SKELETON_DURATION)
		skeletonClone.Parent = Workspace
		skeletonClone:MoveTo(spawnPosition) --NOTE: Model must be in Workspace

		-- Rise!
		local torso = skeletonClone:FindFirstChild('Torso')
		if torso then
			torso.CFrame = torso.CFrame - Vector3.new(0, 4.5, 0)
			for i = 0, 4.5, 0.45 do
				torso.CFrame = torso.CFrame + Vector3.new(0, i, 0)
				wait(1/30)
			end
		end
	end
end

local function RaiseSkeletons()
	if not Skeleton then -- Try again
		MakeSkeleton()
	end
	for theta = -135, -45, 45 do
		SpawnSkeleton(MyModel.Torso.CFrame:pointToWorldSpace(Vector3.new(math.cos(theta), 0, math.sin(theta)) * SPAWN_RADIUS))
	end
end
