For anyone reading this because they are trying to convert some code with “find part on ray” into the more preferred “raycast” I changed my code to look like this:
while true do
--n=CFrame of character within listd distance of NPC
--mag=the distance limit
--hum=look vector that looks at where the player character was when "near"
local n,mag,hum=near()--calls near function and that functions sets n,mag,hum
if n then --if a character was near, and CFrame was sent
n=n.p--takes position from cFrame without rotational info
if not r then --r is reload debounce, and false means not reloading at moment
---
-- Build a "RaycastParams" object
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {t}--probably t
raycastParams.IgnoreWater = true
---
w=.0001 --w as wait time is reset (this is global)
local dir=h.Position-n--h is NPC gun, n is character cframe
local rayCastTest = workspace:Raycast(t.Torso.Position,(n-t.Torso.Position).Unit*99999, raycastParams)
local aim=n+Vector3.new(math.random(-mag*.008,mag*.008),math.random(-mag*.009,mag*.009),math.random(-mag*.1,mag*.1))*(2+math.random())+hum --this decides where bullet lands with some randomness
if rayCastTest and rayCastTest.Instance and rayCastTest.Instance.Parent and game.Players:GetPlayerFromCharacter(rayCastTest.Instance.Parent)then--checks to see if part that was hit by ray was the character's part
wait(0.4)-- between shots?
h.Fire:Play()--plays gunsound from gun
local rayCastReal = workspace:Raycast(h.Position,(aim-h.Position).Unit*99999, raycastParams)
if rayCastReal and rayCastReal.Instance then--raycast was succesful
local h=rayCastReal.Instance.Parent:FindFirstChild'Humanoid'or rayCastReal.Instance.Parent.Parent:FindFirstChild'Humanoid'--h is humanoid of character hit
if h and h.Health-10>0 then--do damage if character won't die
h.Health=h.Health-10
elseif h then--character has less health than damage so kill it
h.Health=0
h.Parent:BreakJoints()
end
end
if rayCastReal and rayCastReal.Instance then
ammo=ammo-1--reduce ammo for shot
mag=(h.Position-rayCastReal.Instance.Position).magnitude--distance between character humanoid and NPC (t)
b1.Parent,b2.Parent=t,t--parents bullets to npc
b1.CFrame,b2.CFrame=CFrame.new(h.Position:Lerp(rayCastReal.Instance.Position,.375),rayCastReal.Instance.Position),CFrame.new(rayCastReal.Instance.Position:Lerp(h.Position,.125),h.Position)--lerps bullets to character
b1.Mesh.Scale,b2.Mesh.Scale=Vector3.new(.2,.2,mag*.75),Vector3.new(.2,.2,mag/4)--changes appearance of bullets?
delay(.03,function()b1.Parent=nil wait(.03)b2.Parent=nil end)--removes bullets
end
end
wait(.05)
if ammo==0 then
reload()--calls reload but doesn't pass "true" ?? so is then "false" and doesn't reload?
end
end
else
reload(true) --whys it call reload if no "n" cframe?
w=false --why is w now a boolean?
end
end
Sorry if its at all hard to understand. I thought I should post the solution in case it helps someone. Thanks again to all respondents - super appreciated. The tip that raycast is returning a table, and I should print the results of that table, was the tip that allowed me to work and finagle it until it works.
Much thanks to Ortacise, Sanguine-Saracen/Bobbybob2131, and Allanv2/alliedoeihoialt !!