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

Attempt to index number with 'human' error. I'm not sure what happened?

Asked by 3 years ago

In the output, it's saying on line 65 there's an error saying: 'Attempt to index number with 'human'' although I'm quite sure I did it right.

local CollectionService = game:GetService("CollectionService")

local zombies = {}

function updateTarget() -- creating function to update the target
    local humans = CollectionService:GetTagged("Human")
    for _, zombie in pairs(zombies) do
        local target = nil
        local dist = 200
        for _, human in pairs(humans) do
            local root = human.RootPart
            if root and human.Health > 0 and (root.Position - zombie.root.Position).magnitude < dist and human.Parent.Name ~= zombie.char.Name then
                dist = (root.Position - zombie.root.Position).magnitude
                zombie.target = root
            end
        end
    end
end

spawn(function()
    while wait(1) do
        updateTarget()
    end
end)

function findPath(zombie)
    local x = math.random(-50,-50)
    local z = math.random(-50,-50)

    local destination = Vector3.new(x,0,z)

    local path = game:GetService("PathfindingService"):CreatePath()
    path:ComputeAsync(zombie.root.Position,destination)
    local waypoints = path:GetWaypoints()
    local currentTarget = zombie.target

    for _, waypoint in pairs(waypoints) do
        if path.Status == Enum.PathStatus.Success then
            if waypoint.Action == Enum.PathWaypointAction.Jump then
                zombie.human.Jump = true
            end
            zombie.human:MoveTo(waypoint.Position)
            local cooldown = zombie.human.MoveToFinished:Wait(1)
            if not cooldown then
                print("Got stuck!")
                zombie.human.Jump = true
                findPath(zombie)
                break
            end
            if not zombie.target then
                break
            elseif (currentTarget.Position - waypoints[#waypoints].Position).magnitude > 10 or currentTarget ~= zombie.target then
                findPath(zombie)
                break
            end
        end
        print("Path unsuccessful.")
        zombie.human.Jump = true
        findPath(zombie)
    end
end

function moveHandler(zombie)
    while wait(1) do
        if zombie.human.Health <= 0 then
            break
        end
        if zombie.target then
            findPath(zombie)
        end
    end
end

function tagHuman(instance)
    local human = instance:FindFirstChildWhichIsA("Humanoid")
    if human then
        CollectionService:AddTag(human,"Human")
    end
end 

function removeZombie(zombie)
    local index = table.find(zombies,zombie)
    table.remove(zombies,index)
    wait(5)
    zombie.char:Destroy()
    zombie.clone.Parent = workspace
end

function addZombie(zombieHumanoid)
    table.insert(zombies,{
        char = zombieHumanoid.Parent,
        root = zombieHumanoid.RootPart,
        human = zombieHumanoid,
        target = nil,
        clone = zombieHumanoid.Parent:Clone()
    })
    for _,zombie in pairs(zombies) do
        if zombie.human == zombieHumanoid then
            zombie.human.Died:Connect(function() removeZombie(zombie) end)
for i, v in pairs(zombie.char:GetDescendants()) do
    if v:IsA("BasePart") and v:CanSetNetworkOwnership() then
        v:SetNetworkOwnership(nil)
    end
end
spawn(moveHandler, zombie)
break
end
end
end

workspace.ChildAdded:Connect(tagHuman)
workspace.ChildAdded:Connect(function() print("Hi") end)

CollectionService:GetInstanceAddedSignal("Human"):Connect(function(zombieHumanoid)
    addZombie(zombieHumanoid)   
end)

function initialize()
    for _, v in pairs(CollectionService:GetTagged("Zombie")) do
        local found = false
        for _, x in pairs(zombies) do
            if x.human == v then
                found = true
            end
        end
        if not found then
            addZombie(v)    
        end
    end
end

initialize()

function attack(zombie)
    local human = zombie.target.Parent:WaitForChild("Humanoid")
    human:TakeDamage(math.random(15,25))
end

spawn(function()
    while wait(0.5) do
        for _, zombie in pairs(zombies) do
            if zombie.target then
                if (zombie.target.Position -  zombie.root.Position).magnitude < 5 then
                    attack(zombie)
                end
            end
        end
    end
end)

1 answer

Log in to vote
1
Answered by
NGC4637 602 Moderation Voter
3 years ago

I think you should use humanoid.Died Event.

So here is the moveHandler function rewritten:

function moveHandler(zombie)
    while wait(1) do
        zombie:FindFirstChild("human").Died:Connect(function()
            break
        end)
        if zombie.target then
            findPath(zombie)
        end
    end
end

see if it errors this time.

0
now it just says: attempt to index number with FindFirstChild lol ISkyLordDoge 37 — 3y
0
what if you change :FindFirstChild("human") to :FindFirstChildWhichIsA("Humanoid") ? NGC4637 602 — 3y
Ad

Answer this question