Need help with gun raycasting

Hi so the problem is that sometimes the bullet goes in the wrong direction when there’s something in between the player and the point it touches, how can I fix this?

Script:

local m = mouse.Hit.Position
     local Inacuracy = config.Spread * (m - tool.Origin.CFrame.Position).magnitude / 10
     for i = 1,times,1 do
          local x = m.X + (math.random(-config.Spread,config.Spread) / 10)
          local y = m.Y + (math.random(-config.Spread,config.Spread) / 10)
          local z = m.Z + (math.random(-config.Spread,config.Spread) / 10)
          local ray = workspace:Raycast(script.Parent.Origin.Position,(Vector3.new(x,y,z)))
          if ray then
               if ray.Instance then
                    gunEvent:FireServer(orgpos,ray.Position,config)
               else
                    gunEvent:FireServer(orgpos,Vector3.new(x,y,z),config)
               end
          else
               gunEvent:FireServer(orgpos,Vector3.new(x,y,z),config)
          end
     end

try using

(Vector3.new(x,y,z)-script.Parent.Origin.Position).Unit*9999 --- 9999 is the max range
1 Like

Lifesaver, thank’s a lot mate.