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

My script for a Zombie doesn't work. When I test play the game, the zombie doesn't move. Why?

Asked by
3DMage -2
6 years ago
local rootPart = script. Parent.HumanoidRootPart
local human = script.Parent.Humanoid

function findTarget()
    local dist = 200
    local target = nil
    for i,v in pairs (workspace:GetChildren()) do
        local humanoid = v:FindFirstChild("Humanoid")
        local head = v:FindFirstChild("Head")
        if humanoid and head and v.Name ~= "Zombie" then
            if  (head.Position - rootPart.Position).magnitude < dist and humanoid.Health > 0 then
                dist = (head.Position - rootPart.Position).magnitude
                target = head
            end
        end
    end
    return target
end

while wait(1) do
    local head = findTarget()

    if head then
        local ray = Ray.new(rootPart.Position, (head.Position - rootPart.Position).Unit * 200)
        local hit,position = workspace.FindPartOnRayWithIgnoreList(ray,{script.Parent})
        if hit then 
            print(hit.Name)
            if hit:IsDescendantOf(head.Parent) then
                print("Success")
                human:MoveTo(head.Position)
            else
                human:MoveTo(head.Position)
                wait(0.4)
                path = game:GetService("PathfindingService"):FindPathAsync(rootPart.Position,head.Position)
                points = path:GetWaypoints()
                if path.Status == Enum.PathStatus.Success then
                    for i,v in pairs(points) do
                        human:MoveTo(v.Position)
                        human.MoveToFInished:wait(2)
                        if v.Action == Enum.PathWaypointAction.Jump then
                            human.Jump = true
                        end
                        if (points(#points).Position - head.Position).magnitude > 15 then
                            break
                        end
                    end
                else
                    human.MoveTo(head.Position) 
                end 
            end     
        end
    end
end

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Your code has 2 things wrong with it:

There is a space in-between script. Parent.HumanoidRootPart so it can not index properly

You are using .FindPartOnRayWithIgnoreList instead of :FindPartOnRayWithIgnoreList

This should work:

local rootPart = script.Parent.HumanoidRootPart
local human = script.Parent.Humanoid

function findTarget()
    local dist = 200
    local target = nil
    for i,v in pairs (workspace:GetChildren()) do
        local humanoid = v:FindFirstChild("Humanoid")
        local head = v:FindFirstChild("Head")
        if humanoid and head and v.Name ~= "Zombie" then
            if  (head.Position - rootPart.Position).magnitude < dist and humanoid.Health > 0 then
                dist = (head.Position - rootPart.Position).magnitude
                target = head
            end
        end
    end
    return target
end

while wait(1) do
    local head = findTarget()

    if head then
        local ray = Ray.new(rootPart.Position, (head.Position - rootPart.Position).Unit * 200)
        local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
        if hit then 
            print(hit.Name)
            if hit:IsDescendantOf(head.Parent) then
                print("Success")
                human:MoveTo(head.Position)
            else
                human:MoveTo(head.Position)
                wait(0.4)
                path = game:GetService("PathfindingService"):FindPathAsync(rootPart.Position,head.Position)
                points = path:GetWaypoints()
                if path.Status == Enum.PathStatus.Success then
                    for i,v in pairs(points) do
                        human:MoveTo(v.Position)
                        human.MoveToFInished:wait(2)
                        if v.Action == Enum.PathWaypointAction.Jump then
                            human.Jump = true
                        end
                        if (points(#points).Position - head.Position).magnitude > 15 then
                            break
                        end
                    end
                else
                    human.MoveTo(head.Position) 
                end 
            end     
        end
    end
end

Ad

Answer this question