----------------------------------------------------------------------
--------------------[ BULLET IMPACT HANDLING ]------------------------
----------------------------------------------------------------------

local createBulletImpact = script:WaitForChild("createBulletImpact")
createBulletImpact.OnServerEvent:connect(function(_, H, P, N, D, humanoidFound, gunIgnore, S)
	local surfaceCF = CF(P, P + N)
	----------------------------------------------------------------------------------
	--Creating the bullet hole--------------------------------------------------------
	----------------------------------------------------------------------------------
	if S.bulletHoles and (not humanoidFound) then
		local Hole = Instance.new("Part")
		Hole.Transparency = 1
		Hole.Anchored = true
		Hole.CanCollide = false
		Hole.FormFactor = "Custom"
		Hole.Size = V3(1, 1, 0.2)
		Hole.TopSurface = 0
		Hole.BottomSurface = 0
		local Mesh = Instance.new("BlockMesh")
		Mesh.Offset = V3(0, 0, -0.05)
		Mesh.Scale = V3(S.holeSettings.Size, S.holeSettings.Size, 0)
		Mesh.Parent = Hole
		local Decal = Instance.new("Decal")
		Decal.Face = Enum.NormalId.Front
		Decal.Texture = S.holeSettings.Texture
		Decal.Parent = Hole
		Hole.Parent = gunIgnore
		Hole.CFrame = surfaceCF
		if (not H.Anchored) then
			local Weld = Instance.new("Weld", Hole)
			Weld.Part0 = H
			Weld.Part1 = Hole
			Weld.C0 = H.CFrame:toObjectSpace(surfaceCF)
			Hole.Anchored = false
		end
		delay(S.holeSettings.visibleTime, function()
			if S.holeSettings.disappearTime > 0 then
				local t0 = tick()
				while true do
					local Alpha = math.min((tick() - t0) / S.holeSettings.disappearTime, 1)
					Decal.Transparency = numLerp(0, 1, Alpha)
					if Alpha == 1 then break end
					wait()
				end
				Hole:Destroy()
			else
				Hole:Destroy()
			end
		end)
	end
	----------------------------------------------------------------------------------
	--Creating the spark effect-------------------------------------------------------
	----------------------------------------------------------------------------------
	if S.bulletSparks and (not humanoidFound) and inList(H.Material, S.sparkSettings.Materials) then
		local Sparks = Instance.new("Part")
		Sparks.Transparency = 1
		Sparks.Anchored = true
		Sparks.CanCollide = false
		Sparks.FormFactor = "Custom"
		Sparks.Size = V3(1, 1, 1)
		Sparks.TopSurface = 0
		Sparks.BottomSurface = 0
		
		local Particles = Instance.new("ParticleEmitter")
		Particles.Color = ColorSequence.new(S.sparkSettings.Color)
		Particles.LightEmission = 1
		Particles.Size = NumberSequence.new(S.sparkSettings.Size)
		Particles.Texture = S.sparkSettings.Texture
		Particles.Transparency = NumberSequence.new(
			{
				NumberSequenceKeypoint.new(0, 0.25, 0.25);
				NumberSequenceKeypoint.new(1, 1);
			}
		)
		Particles.Acceleration = V3(0, -196.2, 0)
		Particles.EmissionDirection = Enum.NormalId.Front
		Particles.Lifetime = NumberRange.new(S.sparkSettings.Lifetime - 0.05, S.sparkSettings.Lifetime + 0.05)
		Particles.Rate = S.sparkSettings.Rate
		Particles.RotSpeed = NumberRange.new(360)
		Particles.Speed = NumberRange.new(S.sparkSettings.Speed - 5, S.sparkSettings.Speed + 5)
		Particles.VelocitySpread = S.sparkSettings.Spread
		Particles.Parent = Sparks
		
		Sparks.Parent = gunIgnore
		Sparks.CFrame = surfaceCF
		if (not H.Anchored) then
			local Weld = Instance.new("Weld", Sparks)
			Weld.Part0 = H
			Weld.Part1 = Sparks
			Weld.C0 = H.CFrame:toObjectSpace(surfaceCF)
			Sparks.Anchored = false
		end
		delay(0.1, function()
			Particles.Enabled = false
			wait(S.sparkSettings.Lifetime + 0.05)
			Sparks:Destroy()
		end)
	end
	----------------------------------------------------------------------------------
	--Creating the smoke effect-------------------------------------------------------
	----------------------------------------------------------------------------------
	if S.bulletSmoke and (not humanoidFound) then
		local Smoke = Instance.new("Part")
		Smoke.Transparency = 1
		Smoke.Anchored = true
		Smoke.CanCollide = false
		Smoke.FormFactor = "Custom"
		Smoke.Size = V3(1, 1, 1)
		Smoke.TopSurface = 0
		Smoke.BottomSurface = 0
		
		local Particles = Instance.new("ParticleEmitter")
		Particles.Color = ColorSequence.new(S.smokeSettings.objColor and H.Color or S.smokeSettings.Color)
		Particles.LightEmission = 0
		Particles.Size = NumberSequence.new(
			{
				NumberSequenceKeypoint.new(0, S.smokeSettings.Size.Start);
				NumberSequenceKeypoint.new(1, S.smokeSettings.Size.End);
			}
		)
		Particles.Texture = S.smokeSettings.Texture
		Particles.Transparency = NumberSequence.new(
			{
				NumberSequenceKeypoint.new(0, S.smokeSettings.startTransparency);
				NumberSequenceKeypoint.new(0.5, 0.75 * S.smokeSettings.startTransparency + 0.25);
				NumberSequenceKeypoint.new(1, 1);
			}
		)
		Particles.Acceleration = V3(0, -196.2, 0)
		Particles.EmissionDirection = Enum.NormalId.Front
		Particles.Lifetime = NumberRange.new(S.smokeSettings.Lifetime - 0.05, S.smokeSettings.Lifetime + 0.05)
		Particles.Rate = S.smokeSettings.Rate
		Particles.Rotation = NumberRange.new(0, 360)
		Particles.RotSpeed = NumberRange.new(10)
		Particles.Speed = NumberRange.new(S.smokeSettings.Speed - 5, S.smokeSettings.Speed + 5)
		Particles.VelocitySpread = S.smokeSettings.Spread
		Particles.Parent = Smoke
		
		Smoke.Parent = gunIgnore
		Smoke.CFrame = surfaceCF
		if (not H.Anchored) then
			local Weld = Instance.new("Weld", Smoke)
			Weld.Part0 = H
			Weld.Part1 = Smoke
			Weld.C0 = H.CFrame:toObjectSpace(surfaceCF)
			Smoke.Anchored = false
		end
		delay(0.1, function()
			Particles.Enabled = false
			wait(S.smokeSettings.Lifetime + 0.05)
			Smoke:Destroy()
		end)
	end
end)