FastCast bullet offset

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want the bullets to come out of the barrel, it bothers me when it’s offset

  2. What is the issue? Include screenshots / videos if possible!
    it does come out of the barrel sometimes but not every time
    Untitled Game Roblox Studio 2023 07 07 16 37 54 - YouTube

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I just copied this, I don’t know how it works
    Fastcast cosmetic bullet not showing if target is close - #5 by dthecoolest

this is my code

local RNG = Random.new()
local FastCast = require(game.ReplicatedStorage.Modules.FastCastRedux)
local CSFire = game.ReplicatedStorage.Events.CSFire
local SSFire = game.ReplicatedStorage.Events.SSFire
local TweenService = game:GetService("TweenService")

local Debris = game:GetService("Debris")

local BulletsFolder = workspace:FindFirstChild("BulletFolder") or Instance.new("Folder", workspace)
BulletsFolder.Name = "BulletFolder"

local bulletFrameNumberDictionary = {} 

local Caster = FastCast.new()

local CastParams = RaycastParams.new()
CastParams.FilterType = Enum.RaycastFilterType.Exclude
CastParams.IgnoreWater = true

local CastBehavior = FastCast.newBehavior()
CastBehavior.RaycastParams = CastParams
CastBehavior.Acceleration = Vector3.new(0, -game.Workspace.Gravity/2, 0)
CastBehavior.AutoIgnoreContainer = false
CastBehavior.MaxDistance = 5000
CastBehavior.CosmeticBulletContainer = BulletsFolder

local function RayUpdated(activateCaster, segmentOrigin, segmentDirection, length, segmentVelocity, bullet: BasePart)	
	local BulletLength = bullet.Size.Z / 2
	local goalCF
	task.spawn(function()
		if bulletFrameNumberDictionary[bullet] ~= nil then
			goalCF = CFrame.lookAt(segmentOrigin, segmentOrigin + segmentDirection)
				* CFrame.new(0, 0, -(length/2 - BulletLength))    
		else
			bulletFrameNumberDictionary[bullet] = 0
			goalCF = CFrame.lookAt(segmentOrigin, segmentOrigin + segmentDirection)   
		end
	end)
	bulletFrameNumberDictionary[bullet] += 1
	bullet:PivotTo(goalCF)
end

local function CleanUpBullet(activeCast)
	local bullet = activeCast.RayInfo.CosmeticBulletObject
	
	if bulletFrameNumberDictionary[bullet] <= 2 then
		task.wait()
		task.wait()
		task.wait()
	end
	
	bulletFrameNumberDictionary[bullet] = nil
	task.spawn(function()
		local trails = {}

		for i, v in ipairs(bullet:GetChildren()) do
			if v:IsA("Trail") then
				table.insert(trails, v)
			end
		end

		for i, v in ipairs(trails) do
			v.Enabled = false
		end
	end)
	bullet.Transparency = 1
	Debris:AddItem(bullet,5)
end

local function OnRayHit(Cast, Result, Velocity, Bullet)
	local bullet = Cast.RayInfo.CosmeticBulletObject
	if bulletFrameNumberDictionary[bullet] <= 2 and bullet then
		local direction = Result.Position - bullet.Position
		local originalPosition = bullet.Position
		task.wait()
		bullet.CFrame = bullet.CFrame + direction*0.5
		task.wait()
		bullet.CFrame = bullet.CFrame + direction*0.5
		CleanUpBullet(Cast)
	end
end

SSFire.Event:Connect(function(Settings, Gun, MousePos)
	CastBehavior.CosmeticBulletTemplate = Settings.Bullet
	CastParams.FilterDescendantsInstances = {Gun.Parent, BulletsFolder}
	
	local Origin = Settings.Aim.WorldPosition
	local Direction = CFrame.new(Origin, MousePos).LookVector * 1000 
	local SpreadAngle = math.rad(math.random(Settings.MinSpread, Settings.MaxSpread))
	local FinalSpread = CFrame.Angles((0.5 - math.random()) * SpreadAngle,
		(0.5 - math.random()) * SpreadAngle,
		(0.5 - math.random()) * SpreadAngle) * Direction
	
	local ActiveCast = Caster:Fire(Origin, FinalSpread, Settings.Range, CastBehavior)
	task.spawn(function()
		local Bullet = ActiveCast.RayInfo.CosmeticBulletObject
		task.spawn(function()
			local trails = {}

			for i, v in ipairs(Bullet:GetChildren()) do
				if v:IsA("Trail") then
					table.insert(trails, v)
				end
			end

			for i, v in ipairs(trails) do
				v.Enabled = true
			end
		end)
		local Info = TweenInfo.new(
			1, 
			Enum.EasingStyle.Sine, 
			Enum.EasingDirection.InOut, 
			0, 
			false, 
			0
		)

		local Goal = {
			Size = Vector3.new(Bullet.Size.X, Bullet.Size.Y, 100)
		}
		
		TweenService:Create(Bullet, Info, Goal):Play()
	end)
	CastBehavior.CosmeticBulletTemplate = nil
end)

Caster.LengthChanged:Connect(RayUpdated)
Caster.CastTerminating:Connect(CleanUpBullet)
Caster.RayHit:Connect(OnRayHit)