-- Creates "bullet". No projectile motion is actually used. Pistol raytraces to target and creates
-- a tracer trail to the target. Fading trail gives illusion of motion.
local function createBullet(target)

	-- Crate bullet that uses a trail effect for the trail
	local bullet = tool.Bullet:Clone()
	table.insert(ignoreTable, bullet)
	bullet.Parent = workspace
	bullet.CFrame = CFrame.new(muzzle.CFrame.p, target)
	bullet.Trail.Enabled = true
	wait() -- so trail has time to load

	bullet.Transparency = 0
	local missionAccomplished = false

	for i = 1, numSegments do
		if not missionAccomplished then
			local equation  = bullet.CFrame * CFrame.new(0, 0, -segmentLength) * CFrame.Angles(dropAngle, 0, 0)
			local ray = Ray.new(bullet.Position, (equation.p - bullet.Position).unit * segmentLength)
			local part, pos, normal, material = workspace:FindPartOnRayWithIgnoreList(ray, ignoreTable)
			if part and part.Parent:FindFirstChild("Humanoid") then
				missionAccomplished = true
				bullet.HitSound:Play()
				hitEvent:FireClient(game.Players:GetPlayerFromCharacter(tool.Parent))
				local hum = part.Parent.Humanoid
				if part.Name == "Head" and configs.headshotInstaKill == true then
					local neck = part.Parent:FindFirstChild("Neck", true)
					neck:Destroy()
				elseif  part.Name == "Head" then
					hum:TakeDamage(headDamage)
				elseif part.Name == "Torso" or part.Name == "UpperTorso" or part.Name == "LowerTorso" then -- Names to account for R6 and R15
					hum:TakeDamage(torsoDamage)
				else
					hum:TakeDamage(otherDamage)
				end


				bullet.CFrame = CFrame.new(pos, equation.p)
				local weld = Instance.new("Weld", bullet)
				weld.C0 = bullet.CFrame:inverse() * part.CFrame
				weld.Part0 = bullet
				weld.Part1 = part
				bullet.Anchored = false
				--bullet.Trail.Enabled = false
				bullet.Transparency = 1
				if configs.blood == true then
					bullet.EffectAttachment.BloodEmitter:Emit(20)
				end
				wait(1)
				for ii, v in pairs(ignoreTable) do
					if v == bullet then
						table.remove(ignoreTable, ii)
					end
				end
				bullet:Destroy()
			elseif part then
				missionAccomplished = true
				bullet.CFrame = CFrame.new(pos, equation.p)
				--bullet.Trail.Enabled = false
				bullet.Transparency = 1
				bullet.ThudSound:Play()
				if hitParticles and part.Transparency and part.Transparency < 1 then --<<<<<<<<<< CHANGES HERE <<<<<<<<<<<<<<<<<<
					if part.ClassName ~= "Terrain" then
						bullet.EffectAttachment.DebrisEmitter.Color = ColorSequence.new(part.Color)
					elseif material == Enum.Material.Grass
						or material == Enum.Material.LeafyGrass
						or material == Enum.Material.Ground
						or material == Enum.Material.Mud
						or material == Enum.Material.Cobblestone then
						bullet.EffectAttachment.DebrisEmitter.Color = ColorSequence.new(Color3.new(61/255, 51/255, 44/255))
					elseif material == Enum.Material.Asphalt
						or material == Enum.Material.Concrete
						or material == Enum.Material.Pavement
						or material == Enum.Material.Slate then
						bullet.EffectAttachment.DebrisEmitter.Color = ColorSequence.new(Color3.new(100/255, 99/255, 93/255))
					elseif material == Enum.Material.Limestone
						or material == Enum.Material.Sand
						or material == Enum.Material.WoodPlanks then
						bullet.EffectAttachment.DebrisEmitter.Color = ColorSequence.new(Color3.new(172/255, 153/255, 116/255))
					elseif material == Enum.Material.Brick or material == Enum.Material.Sandstone then
						bullet.EffectAttachment.DebrisEmitter.Color = ColorSequence.new(Color3.new(165/255, 114/255, 95/255))
					elseif material == Enum.Material.Rock then
						bullet.EffectAttachment.DebrisEmitter.Color = ColorSequence.new(Color3.new(93/255, 96/255, 103/255))
					elseif material == Enum.Material.Basalt or material == Enum.Material.CrackedLava then
						bullet.EffectAttachment.DebrisEmitter.Color = ColorSequence.new(Color3.new(22/255, 11/255, 10/255))
					elseif material == Enum.Material.Salt or material == Enum.Material.Snow then
						bullet.EffectAttachment.DebrisEmitter.Color = ColorSequence.new(Color3.new(190/255, 180/255, 190/255))
					elseif material == Enum.Material.Glacier or material == Enum.Material.Ice then
						bullet.EffectAttachment.DebrisEmitter.Color = ColorSequence.new(Color3.new(162/255, 194/255, 217/255))
					end
					bullet.EffectAttachment.DebrisEmitter:Emit(10)
				end
				wait(1)
				for ii, v in pairs(ignoreTable) do
					if v == bullet then
						table.remove(ignoreTable, ii)
					end
				end
				bullet:Destroy()
			else
				bullet.CFrame = equation
			end
			wait(waitBetweenSegments)
		end
	end
	return
end