#someone help me fix the script

1 messages · Page 1 of 1 (latest)

fossil warren
#
local zombiehumanoid = script.Parent.Humanoid

local function findTarget()
    local agrodistance = 100
    local target = nil
    for i , v in pairs(game.Workspace:GetChildren()) do
        local human = v:FindFirstChild("Humanoid")
        local torso = v:FindFirstChild("Torso")
        if human and torso and v ~= script.Parent then
            if (mytorso.Position - torso.Position).magntiude < agrodistance then
                agrodistance = (mytorso.Position - torso.Position).magnitude
                target = torso
                
            end
        end
        return target
    end
end


while wait() do
    local torso = findTarget()
    if torso then
        zombiehumanoid:MoveTo(torso.Position)
    end
end ```
#

i hope somone have soultion

fast quartz
#

local myTorso = script.Parent:FindFirstChild("Torso") or script.Parent:FindFirstChild("UpperTorso")
local zombieHumanoid = script.Parent:FindFirstChild("Humanoid")

local function findTarget()
local maxDistance = 100 -- how far the zombie can "see"
local closestDistance = maxDistance
local target = nil

for _, v in pairs(workspace:GetChildren()) do
    local human = v:FindFirstChild("Humanoid")
    local torso = v:FindFirstChild("Torso") or v:FindFirstChild("UpperTorso")

    if human and torso and v ~= script.Parent then
        local dist = (myTorso.Position - torso.Position).magnitude
        if dist < closestDistance then
            closestDistance = dist
            target = torso
        end
    end
end

return target

end

while task.wait(0.1) do -- runs every 0.1 seconds
local targetTorso = findTarget()
if targetTorso then
zombieHumanoid:MoveTo(targetTorso.Position)
end
end
Here

worldly violet
# fossil warren i hope somone have soultion

effort in == effort out:

The issue with your code is in the findTarget function. The return target line is inside the for loop, which means it will return the first target it finds, and then immediately exit the function. This is not what you want, because you want to find the closest target to the zombie's torso.

Here's the corrected code:

local mytorso = script.Parent.Torso
local zombiehumanoid = script.Parent.Humanoid

local function findTarget()
    local agrodistance = math.huge -- Initialize with a large value
    local target = nil
    for i, v in pairs(game.Workspace:GetChildren()) do
        local human = v:FindFirstChild("Humanoid")
        local torso = v:FindFirstChild("Torso")
        if human and torso and v ~= script.Parent then
            local distance = (mytorso.Position - torso.Position).magnitude
            if distance < agrodistance then
                agrodistance = distance
                target = torso
            end
        end
    end
    return target
end

while wait() do
    local torso = findTarget()
    if torso then
        zombiehumanoid:MoveTo(torso.Position)
    end
end```

> In this corrected version, I added local distance = (mytorso.Position - torso.Position).magnitude to calculate the distance to the target, and then compared that distance to agrodistance. If the new distance is smaller, I updated agrodistance and target. This way, the function will find the closest target to the zombie's torso and return it. I also initialized agrodistance with a large value (math.huge) to ensure that the first target found will always be closer than the initial distance.
> 
> Sources:
> 
> Humanoid

special thanks roblox assistant ai.

low effort questions get low effort answers.