----------------------------------------------------------------------
--------------------[ BLOOD HANDLING ]--------------------------------
----------------------------------------------------------------------


local createBlood = script:WaitForChild("createBlood")
createBlood.OnServerEvent:connect(function(_, H, P, D, gunIgnore, S)
	local bloodCF = CF(P, P + D) * CFANG(RAD(-90), 0, 0)
	local Blood = Instance.new("Part")
	Blood.Transparency = 1
	Blood.Anchored = true
	Blood.CanCollide = false
	Blood.FormFactor = "Custom"
	Blood.Size = V3(0.2, 1, 0.2)
	Blood.TopSurface = 0
	Blood.BottomSurface = 0
	
	local Particles = Instance.new("ParticleEmitter")
	Particles.Color = ColorSequence.new(S.bloodSettings.Color)
	Particles.LightEmission = 0
	Particles.Size = NumberSequence.new(S.bloodSettings.Size)
	Particles.Texture = S.bloodSettings.Texture
	Particles.Transparency = NumberSequence.new(
		{
			NumberSequenceKeypoint.new(0, S.bloodSettings.startTransparency);
			NumberSequenceKeypoint.new(1, 1);
		}
	)
	Particles.EmissionDirection = Enum.NormalId.Top
	Particles.Lifetime = NumberRange.new(S.bloodSettings.Lifetime - 0.05, S.bloodSettings.Lifetime + 0.05)
	Particles.Rate = S.bloodSettings.Rate
	Particles.Rotation = NumberRange.new(0, 90)
	Particles.Speed = NumberRange.new(S.bloodSettings.Speed)
	Particles.VelocitySpread = S.bloodSettings.Spread
	Particles.Parent = Blood
	
	Blood.Parent = gunIgnore
	Blood.CFrame = bloodCF
	if (not H.Anchored) then
		local Weld = Instance.new("Weld", Blood)
		Weld.Part0 = H
		Weld.Part1 = Blood
		Weld.C0 = H.CFrame:toObjectSpace(bloodCF)
		Blood.Anchored = false
	end
	delay(0.15, function()
		Particles.Enabled = false
		wait(S.bloodSettings.Lifetime + 0.05)
		Blood:Destroy()
	end)
end)
