Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Where should I place these scripts In my pathfinding?

Hello! I was wondering where I should place these scripts In my pathfinding, anyway I am making a zombie staff that summons zombies and those zombies will target players (And NPCS probably) but I don't know where I should place these:

Also: The zombies are NPCS cloned from ReplicatedStorage

Pathfinding:

local NPC = script.Parent
local Pathfinding = game:GetService("PathfindingService")
local player

game.Players.PlayerAdded:Connect(function(client)
    player = client
end)

repeat wait() until player ~= nil

while wait() do
    local path = Pathfinding:CreatePath()

    path:ComputeAsync(NPC.Torso.Position, player.Character.PrimaryPart.Position)

    if path.Status == Enum.PathStatus.Success then

        local Positions = path:GetWaypoints()

        for i,v in pairs(Positions) do

            local position = Instance.new("Part")

            position.Parent = game.Workspace

            position.Size = Vector3.new(1,1,1)

            position.Position = v.Position

            position.Transparency = 1

            NPC.Humanoid:MoveTo(position.Position)

            NPC.Humanoid.MoveToFinished:Wait(2)

            position:Destroy()

        end

    end
end

Script 1:

if torso == thisplayerstorso then return end 

Script 2:

local Creator = script.Parent.Creator
function GetPlayerNames()
    local players = game:GetService('Players'):GetChildren()
    local name = nil
    for _, v in pairs(players) do
        if v:IsA'Player' then
            name = tostring(v.Name)
        end
    end
    return name
end
function GetTorso(part)
    local chars = game.Workspace:GetChildren()
    local torso = nil
    for _, v in pairs(chars) do
        if v:IsA'Model' and v ~= script.Parent and v.Name == GetPlayerNames() and not v.Name == Creator.Value then
            local charRoot = v:FindFirstChild'HumanoidRootPart'
            if (charRoot.Position - part).magnitude < SearchDistance then
                torso = charRoot
            end
        end
    end
    return torso
end
while wait() do
    local path = Pathfinding:CreatePath()

    path:ComputeAsync(NPC.Torso.Position, GetTorso(NPC.Torso.Position))

    if path.Status == Enum.PathStatus.Success then

        local Positions = path:GetWaypoints()

        for i,v in pairs(Positions) do

            local position = Instance.new("Part")

            position.Parent = game.Workspace

            position.Size = Vector3.new(1,1,1)

            position.Position = v.Position

            position.Transparency = 1

            NPC.Humanoid:MoveTo(position.Position)

            NPC.Humanoid.MoveToFinished:Wait(2)

            position:Destroy()

        end

    end
end

1 answer

place this script inside of the zombie

local Creator = script.Parent.Creator
function GetPlayerNames()
    local players = game:GetService('Players'):GetChildren()
    local name = nil
    for _, v in pairs(players) do
        if v:IsA'Player' then
            name = tostring(v.Name)
        end
    end
    return name
end
function GetTorso(part)
    local chars = game.Workspace:GetChildren()
    local torso = nil
    for _, v in pairs(chars) do
        if v:IsA'Model' and v ~= script.Parent and v.Name == GetPlayerNames() and not v.Name == Creator.Value then
            local charRoot = v:FindFirstChild'HumanoidRootPart'
            if (charRoot.Position - part).magnitude < SearchDistance then
                torso = charRoot
            end
        end
    end
    return torso
end
while task.wait() do
    local path = Pathfinding:CreatePath()

    path:ComputeAsync(NPC.Torso.Position, GetTorso(NPC.Torso.Position))--its gonna make a radius around the npc torso position to check for players

    if path.Status == Enum.PathStatus.Success then

        local Positions = path:GetWaypoints()

        for i,v in pairs(Positions) do

            local position = Instance.new("Part")

            position.Parent = game.Workspace

            position.Size = Vector3.new(1,1,1)

            position.Position = v.Position

            position.Transparency = 1

            NPC.Humanoid:MoveTo(position.Position)

            NPC.Humanoid.MoveToFinished:Wait(2)

            position:Destroy()

        end

    end
end

idk what your spawning script looks like but you can add this line when spawning this will ensure that the zombie would not attack you

if Zombie:FindFirstChild("Creator") then
      Zombie:FindFirstChild("Creator").Value = Tool.Parent.Name --Tool > Character > Name
end

8 comments

0

I currently don't have a spawning script, but It will probably just use the :Clone() command

0

Also In line 20 the words "SearchDistance" has a orange line In the bottom should I worry about that?

0

Okay here are other errors: 1, It says that the creator function cannot be founded I thought I was a StringValue, so I added a string value and rename It to Creator, It did work but now the output says " Argument 2 missing or nil"

0

Forgot to tell u that the previous comment was for the npc

0

SearchDistance is the range set it to Math.huge if you want the zombie to find you across the map

1

what line of the script is it that caused Argument 2 missing or nil

0

Line 30 In the pathfinding script

0

Pathfinding:CreatePath() does not have the agent params

This discussion is preserved as it was. New answers, comments, and votes are closed.