----------------------------------------------------------------------
--------------------[ TRAIL HANDLING ]--------------------------------
----------------------------------------------------------------------


local createTrail = script:WaitForChild("createTrail")
createTrail.OnServerEvent:connect(function(_, Origin, P, gunIgnore, S)
	local Trail = Instance.new("Part")
	Trail.BrickColor = S.trailSettings.Color
	Trail.Transparency = S.trailSettings.Transparency
	Trail.Anchored = true
	Trail.CanCollide = false
	Trail.Size = V3(1, 1, 1)
	local Mesh = Instance.new("CylinderMesh")
	Mesh.Offset = V3(0, -(P - Origin).magnitude / 2, 0)
	Mesh.Scale = V3(S.trailSettings.Thickness, (P - Origin).magnitude, S.trailSettings.Thickness)
	Mesh.Parent = Trail
	Trail.Parent = gunIgnore
	Trail.CFrame = CF(Origin, P) * CFANG(RAD(90), 0, 0)
	delay(S.trailSettings.visibleTime, function()
		if S.trailSettings.disappearTime > 0 then
			local t0 = tick()
			while true do
				local Alpha = math.min((tick() - t0) / S.trailSettings.disappearTime, 1)
				Trail.Transparency = numLerp(S.trailSettings.Transparency, 1, Alpha)
				if Alpha == 1 then break end
				wait()
			end
			Trail:Destroy()
		else
			Trail:Destroy()
		end
	end)
end)
