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

Why is my pathfinding zombie not going to any players?

Asked by 6 years ago

Hi, all. I have a script in a Humanoid that is supposed to find a random player and use Roblox's pathfinding service to maneuver its way around walls to get to the player.

But the Humanoid doesn't go anywhere, and there are no errors in the output.

Here's the script inside the Humanoid:

local root = script.Parent.Parent.HumanoidRootPart -- Get the zombie's HumanoidRootPart

function moveToPlayer() -- Declare the function
    local players = game.Players:GetPlayers() -- Get a list of players
    for _, each in ipairs(game.Players:GetPlayers()) do -- Find a random player
        local char = each:FindFirstChild("Character") -- Get player's character...
        if char then -- ...and check if it exists
            local human = char.Humanoid -- Get the char's humanoid
            if human.Health > 0 then -- Check if the humanoid still alive
                local pRoot = human.Parent.HumanoidRootPart -- Get the character's HumanoidRootPart
                local path = game:GetService("PathfindingService"):ComputeRawPathAsync(root.Position, pRoot.Position, 500) -- Compute a path
                local points = path:GetCoordinates() -- Get a list of the coordinates
                for i = 1, #points do
                    script.Parent:MoveTo(points[i]) -- Make the zombie move along the points
                end
            end
        end
    end
end

while wait() do
    moveToPlayer()
end

Please help me. I'll give you a free, imaginary, figurative, nonexistent cookie if you do!

0
Does MoveTo automatically yield, because if not then you should add a wait NexanianStudios 91 — 6y
0
Also, try making the while wait() do loop a bit less frequent NexanianStudios 91 — 6y
0
Why do you comment on everything it's a nuisance to read through code when half of it is a comment frostysubatomiczero 51 — 6y
0
@frosty, Because I'm desperate to fix this so I commented on everything to help me figure it out; I don't do this regularly. Also, if you're just here to criticize my commenting and not help me, I politely ask you to leave. Fireorius 51 — 6y
View all comments (2 more)
0
Also @Nexa, I tried both of those but nothing helped. :( Fireorius 51 — 6y
0
Criticism is what helps you get better snowflake frostysubatomiczero 51 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

There are two problems with the code you have written. The first is that you are running the follow code for every player, rather than choosing a random player. The second is that Humanoid:MoveTo doesn't yield, so in a loop with no waits the zombie will almost instantly start heading to the final point in the list. Below is an example of your code where these problems are corrected.

local root = script.Parent.HumanoidRootPart
local rand = Random.new()

local function Path()
    local players = game.Players:GetPlayers()
    if #players > 0 then
        -- From the list of all players, choose a random player
        local playerToFollow = players[rand:NextInteger(1, #players)]
        if playerToFollow.Character and playerToFollow.Character.Humanoid.Health > 0 then
            local pRoot = playerToFollow.Character.HumanoidRootPart
            local path = game:GetService("PathfindingService"):ComputeRawPathAsync(root.Position, pRoot.Position, 500)
            local points = path:GetPointCoordinates()

            for i = 1, #points do
                script.Parent.Humanoid:MoveTo(points[i])

                repeat -- Wait until we are close to a point before moving to the next
                    local distance = (points[i] - root.Position).magnitude
                    wait()
                until distance < 3
            end
        end
    end
end

-- The number inside wait will determine how long the zombie idles at
-- the end of a path before starting to follow another player
while wait(1) do
    Path()
end
Ad

Answer this question