I am making a weapons system currently, but I have been having a problem that only happens when I have to use a spread value other than 0. If the spread that I am using is 0, all of the shots are fired and are visible. If it is not, I get a problem where the CFrame of the part that I am mutating is not set. It still does not get set even if I set it twice.

I have this Code:

```
function module.visualizeCast(origin, endPos)
local distance: number = (endPos - origin).Magnitude
local CF = CFrame.lookAt(origin, endPos) * CFrame.new(0, 0, -distance / 2)
part.CFrame = CF
print(CF.Position == part.CFrame.Position) -- a test to se if the CF is getting set
part.CFrame = CF
print(CF.Position == part.CFrame.Position) -- a test to se if the CF is getting set
end
```

here is where it is being called:

```
local function getSpread(unit: Vector3, x: number): CFrame
-- x is a number between 0 and 500 that defines accuracy
if x > 500 then
x = 500
end
-- y = mx + b. returns an angle in deg between 0 and 4
local angle = m * x + b
local spreadRotation = RNG:NextNumber(-math.pi, math.pi)
local spreadOffset = RNG:NextNumber(-math.rad(angle), math.rad(angle))
local spreadTransform = CFrame.fromAxisAngle(Vector3.new(math.cos(spreadRotation), math.sin(spreadRotation), 0), spreadOffset)
local unspreadCFrame = CFrame.lookAt(Vector3.new(), unit)
return (unspreadCFrame * spreadTransform).LookVector
end
local nextTarget = workspace.Targets.Targets[tostring(num)].Position
local unitRay = Ray.new(origin, getSpread((nextTarget - origin).Unit, spreadValue.Value))
local hitPos: Vector3, result: RaycastResult = raycaster.getHitPosition(origin, unitRay, num)
bulletVisualizer.visualizeCast(origin, result.Position)
```

```
-- this is the raycaster.lua
function module.raycast(startPos : Vector3, unitRay : Ray, maxDist : number) : RaycastResult
local result : RaycastResult? = workspace:Raycast(startPos, unitRay.Direction * maxDist)
if result then
return result
end
return nil
end
function module.getHitPosition(startPos, unitRay, maxDist)
local result = module.raycast(startPos, unitRay, maxDist)
if result then
return result.Position, result
end
return unitRay.Origin + unitRay.Direction * maxDist, nil
end
```

I cannot figure out what the problem is. Sometimes the part’s CFrame is set, other times it is not. It also only happens when I change the spread value