local start = thisGun.Position
local lookAt = thisGun.Position + thisGun.CFrame.lookVector*900
--print("This gun is at position "..start.x..","..start.y..","..start.z)
--print("Looking at position "..lookAt.x..","..lookAt.y..","..lookAt.z)
local ray = Ray.new(start, (lookAt - start).unit)
local targetHit, targetHitPos = game.Workspace:FindPartOnRay(ray)
local targetHitName
local mag = (thisGun.Position - targetHitPos).magnitude
if targetHit ~= nil and mag < 1000 then
targetHitName = targetHit.Name
else
targetHitName = "nil"
targetPos = lookAt
mag = (thisGun.Position - targetPos).magnitude
end
This is the code I've currently got; the code which actually creates the raycasted line is later, and all works correctly; the issue I'm having is that targetHit and targetHitPos are always nil and the point that is 1 unit in front of the gun. Did I do something incorrect in creating the ray that is creating a ray with length of 1?

3 comments
Ahhhhh wonderful; I'll give that a try later. Thank! And thanks for the style note; but I already knew that. It was just a hack to see if I could get it to work at all. I'm sure you understand that, with your extensive coding background ;)
Just to clarify, the ray can still have a maximum length of 1000, as per the wiki article, correct?
Yes, 1000 is fine. Larger values it will accept too, but they will be effectively reduced to length 1000, and it will put an error in the output (so that's not desirable). I like to err slightly shorter just to be safe, but it's not necessary.