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

The zombie NPC won't do anything, even though I want It to protect you (?)

Asked by 2 years ago

Hello!

Im making a zombie staff that summons a zombie from ReplicatedStorage that targets other people besides you, currently, It does nothing

(Also, there Is a StringValue Inside of the zombie, Its used to get the owner of the zombie staff)

No errors appeared In

Script (Zombie):

local NPC = script.Parent
local Pathfinding = game:GetService("PathfindingService")
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()

    local NPC_Torso
    if NPC:FindFirstChild("UpperTorso") then
        NPC_Torso = NPC.UpperTorso
    else
        NPC_Torso = NPC.Torso
    end
    if GetTorso(NPC_Torso) == nil then
        return
    end
    -- Line 30
    path:ComputeAsync(GetTorso(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

Summoning script:

local tool = script.Parent
local debounce = false

tool.Activated:Connect(function()
    if not debounce then
        debounce = true
        local Zombie = game.ReplicatedStorage.Zombie:Clone()
        Zombie.Parent = game.Workspace
        Zombie.Torso.CFrame = tool.Handle.CFrame
        if Zombie:FindFirstChild("Creator") then
            Zombie:FindFirstChild("Creator").Value = tool.Parent.Name --Tool > Character > Name
        end
    end
end)
0
Congrats to people that actually read through it all! MattVSNNL 620 — 2y

2 answers

Log in to vote
0
Answered by
enes223 327 Moderation Voter
2 years ago

hey you! have you ever heard of enes? if you are in trouble, better call enes!

0
Hey ene can you help me with this? imnotaguest1121 362 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

Make it follow you, by doing Pathfinding or Humanoid:MoveTo().

After that, Make it do a Magnitude check and make a find target function. heres a quick and simple one.

function findTarget()   
    for i, child in pairs(workspace:GetChildren()) do
        if child:IsA("Model") and child:FindFirstChild("Humanoid") and child:FindFirstChild("HumanoidRootPart") and child ~= script.Parent then
            local HumanoidRootPart = child:FindFirstChild("HumanoidRootPart")
            local HumanoidTarg = child:FindFirstChild("Humanoid")
            local pos = (HumanoidRootPartDummy.Position - HumanoidRootPart.Position).Magnitude
            if pos < range and HumanoidRootPart and HumanoidTarg then
                return HumanoidRootPart, HumanoidTarg
            end
        end
    end 
end

Edit what is necessary.

After that, do this:

while wait() do
    local target, humanoid = findTarget()
    if target.Parent.Name ~= "playerNamewhatEva" and humanoid.health ~= 0 then
        *load and play whatever animation u set up*
        humanoid:TakeDamage(amountOfDamage)
        wait(loadYouPlayed.length)
    else
        loadYouPlayed:Stop()
    end
end

Might have some errors to tweak, good luck on that, I guess.

Answer this question