Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Npc raycasting doesnt work when it goes side ways?

Asked by 6 years ago
Edited 6 years ago

I have 2 parts that the npc will walk to. But if you put on of the part a little to the right or left from the other part the ray will not check in from of the npc anymore.

How do i fix this and here is the script:

Its inside of the npc

its on line 7-11

local guard = script.Parent
local target


--Vector3.new(guard.HumanoidRootPart.CFrame.lookVector.X).Unit*50

while true do
        local ray = Ray.new(guard.HumanoidRootPart.CFrame.p,(Vector3.new(guard.HumanoidRootPart.CFrame.lookVector.x,guard.HumanoidRootPart.CFrame.lookVector.y,0) * Vector3.new(guard.HumanoidRootPart.CFrame.X+100,0,guard.HumanoidRootPart.CFrame.Z+100)).unit * 25)
        local part, position = workspace:FindPartOnRay(ray, guard, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Bright red")
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local distance = (guard.HumanoidRootPart.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.1, 0.1, distance)
        beam.CFrame = CFrame.new(guard.HumanoidRootPart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

        game:GetService("Debris"):AddItem(beam, 0.1)
        if part then
            if part.Parent:FindFirstChild("Humanoid") and game.Players:FindFirstChild(part.Parent.Name) then
                --print("FOUND A PLAYER! "..guard.Name)
                guard.MovementScript.Disabled = true
                guard.Humanoid:MoveTo(part.Parent.HumanoidRootPart.Position+ Vector3.new(0,0,2))
                game.Players:FindFirstChild(part.Parent.Name).leaderstats.attension.Value = game.Players:FindFirstChild(part.Parent.Name).leaderstats.attension.Value+1
                if game.Players:FindFirstChild(part.Parent.Name).leaderstats.attension.Value >= 100 then
                    target = part.Parent
                end
            else
                wait(.3)
                guard.MovementScript.Disabled = false
            end
        end
    if target ~= nil then
        if workspace:FindFirstChild(target.Name) then
            guard.Humanoid:MoveTo(target.HumanoidRootPart.Position)
        end
    end
    wait()
end

Please make a comment if you didnt understand the parts and why it didn't work.

0
Line 08 seems cut to me please enlighten it . Thank you arshad145 392 — 6y
0
line 8 is cutted i cant enlighten it its scriptinghelpers that does that to long lines... frederikhome 44 — 6y

Answer this question