--- FUNCTIONS ---


Projectile = function(Character, MousePosition)
	-- turn to cursor

	local Projectile = ServerStorage.Models.ManaBolt:Clone()
	Projectile.Parent = workspace.Debris
	Projectile.CFrame = Character["Mana Missile Staff"].Top.CFrame
	game.Debris:AddItem(Projectile, 10)

	local ProjectileVelocity = Instance.new("BodyVelocity")
	ProjectileVelocity.P = math.huge
	-- Shoot towards victim humanoidrootpart
	ProjectileVelocity.Velocity = (MousePosition - Projectile.CFrame.Position).unit * 100
	ProjectileVelocity.Parent = Projectile

	local Connection
	Connection = Projectile.Hitbox.Touched:Connect(function(Hit)
		local HitCharacter = Hit.Parent
		if not HitCharacter then return end
		if HitCharacter:FindFirstChild("Humanoid") and HitCharacter ~= Character and HitCharacter.Parent == workspace.Enemies then

			Connection:Disconnect()
			HitCharacter.Humanoid:TakeDamage(25)

			Projectile.Hit:Play()
			Projectile.Bolt:Destroy()
			Projectile.ParticleEmitter:Destroy()
			Projectile.Anchored = true
			task.wait(1)
			Projectile:Destroy()

		elseif Hit.Anchored == true then

			Connection:Disconnect()

			Projectile.Hit:Play()
			Projectile.Bolt:Destroy()
			Projectile.ParticleEmitter:Destroy()
			Projectile.Anchored = true
			task.wait(1)
			Projectile:Destroy()

		end
	end)
end


