Direction for raycast is not working

I have been trying to make it where the bullets have spread, but I keep getting the same error again and again saying where the direction is, "attempt to perform arithmetic (umn) on nil. Any way to fix this? I just don’t get this.

(MousePosition is mouse.hit.p and BarrelPos is the orgin of the raycast.)

image

		local Spread = Module.Spread
		local Direction = (CFrame.new(BarrelPos,MousePosition) * CFrame.Angles(math.rad(-Spread+(math.random()*(Spread*2))),math.rad(-Spread+(math.random()*(Spread*2))),0)).lookVector

Please separate your CFrame statements like so below.

local Spread = Module.Spread
local xAxisRotation = math.rad(-Spread+(math.random()*(Spread*2)))
local yAxisRotation = math.rad(-Spread+(math.random()*(Spread*2)))
local spreadRotationCF = CFrame.Angles(xAxisRotation,yAxisRotation,0)
local initialDirectionCF = CFrame.new(BarrelPos,MousePosition)
local Direction = (initialDirectionCF*spreadRotationCF).lookVector

Now you can debug further to see which specific math related line has a variable becoming nil instead of that one specific really long line. I’m guessing Module.Spread is equal to nil somehow causing the arithmetic error.

2 Likes

That’s weird. I been checking that var in my script and it has been looking okay, but I’ll do that, thanks.

update: aquired module on clientside, and transfering modules through remoteevents cause a lot of trouble.

Oh there is something wrong about how the module is aquired by the script, but your solution worked. Thanks!