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

How to fix when enemies health lags in Filtering Enabled?

Asked by 6 years ago

In my game, I have about 50 zombies on the map. Most of the zombies die without lag, however, sometimes the zombies don't die even though the health appears to be 0.

I have two scripts:

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 1000
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("Right Arm")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end

while wait(0.1) do
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end
    if script.Parent ~= nil then 
        if script.Parent.Humanoid.Health <= 30 then
            script.Parent.Humanoid.Health = -1
        end
    end

end

And the respawn script:

name="Humanoid"

robo=script.Parent:clone()

while wait(0.1) do
    if script.Parent.Humanoid.Health<1 then
        wait(1)
        robot=robo:clone()
        robot.Parent=script.Parent.Parent
        robot:makeJoints()
        script.Parent:remove()
    end
end

There was no lag before I changed my game to Filtering Enabled.

Answer this question