#why is this zombie script not working?

18 messages · Page 1 of 1 (latest)

full mountain
#

this script i have has no errors i checked through it using print and everything is being run but its still not working

local zombie = script.Parent
local root = zombie:WaitForChild('HumanoidRootPart')
local humanoid = zombie:WaitForChild('Humanoid')
local players = game:GetService('Players')

local function nearestPlayer()
    local temp = nil
    local distance = 100
    for _,plr in pairs(players:GetPlayers()) do
        if plr then
            local character = plr.Character
            if character then
                local h = character:FindFirstChildWhichIsA('Humanoid')
                if h and h.Health > 0 then
                    local r = character:FindFirstChild('HumanoidRootPart')
                    if r then
                        local dist = (r.Position - root.Position).Magnitude
                        if dist < distance then
                            print("humanoids found")
                            distance = dist
                            temp = r
                        end
                    end
                end
            end
        end
    end
end

humanoid.Touched:Connect(function(hit)
    local h = hit.Parent:FindFirstChildWhichIsA('Humanoid')
    if h and h.Health then
        h:TakeDamage(10)
    end
end)

while true do
    game:GetService('RunService').Heartbeat:Wait()
    local r = nearestPlayer()
    if r then
        humanoid:MoveTo(r.Position)
    end
end
scenic parrot
#

because you use pairs

full mountain
scenic parrot
#

Learn

#

Scripting

full mountain
#

let me read up on it but give me the alternitive

scenic parrot
#

Watch more dev queen videos

#

🥱

full mountain
#

your not helpfull at all

full mountain
digital harbor
#

ipairs

full mountain
digital harbor
#

wdym its not a thing

full mountain
#

i looked it up and nothing came up

digital harbor
#

it is a thing

full mountain
#

is this correct? ```lua
for i,plr in pairs(players:GetPlayers()) do