(This is used by an NPC). SpawnPosition1 is the tip of the weapon, and targetPosition1 is the players Torso it is locked on to.
local function MakeRocket(spawnPosition1,targetPosition1)
--------Randomize targetPosition1
targetPosition1 = Vector3.new(targetPosition1.X + math.random(-3,3),targetPosition1.Y + math.random(-3,3),targetPosition1.Z + math.random(-3,3))
local ray = Ray.new(spawnPosition1, (targetPosition1 - Tool.Handle.CFrame.p).unit * 300)
local hit, position = game.Workspace:FindPartOnRay(ray, MyModel)
local distance = (position - Tool.Handle.CFrame.p).magnitude
local rayPart = Instance.new("Part", MyModel)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("Bright red")
rayPart.Transparency = 0.5
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.TopSurface = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.formFactor = Enum.FormFactor.Custom
rayPart.Size = Vector3.new(0.2, 0.2, distance)
rayPart.CFrame = CFrame.new(Tool.Handle.CFrame.p, position--[[targetPosition1]]) * CFrame.new(0, 0, -distance/2)
--add it to debris so it disappears after 0.1 seconds
rayPart.Touched:connect(function(part)
local hithumanoid = part and part.Parent and part.Parent:FindFirstChild("Humanoid")
if hithumanoid and hityet == false and hithumanoid.Parent ~= MyModel then
hityet = true
local damagehitting = nil
for index, value in pairs(points) do
if distance >= points[index][1] and points[index + 1] and distance <= points[index + 1][1] then
local slope = (points[index + 1][2] - points[index][2])/(points[index + 1][1] - points[index][1])
local yint = points[index][2] - (slope*points[index][1]) --points[index][2]
local damagehitting = slope*distance + yint
if damagehitting >= 0 then
--print(damagehitting)
hithumanoid:TakeDamage(damagehitting)
break
end
end
end
end
end)
DebrisService:AddItem(rayPart, 0.1)
end
The raycast is accurate (as accurate as I want it to be), but for some reason shoots through blocks and the method FindPartOnRay doesn't seem to detect the part even though the "lazer" clearly goes through it. How could I fix this?
