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

My walk script sometimes stops the humanoid for some reason?

Asked by 2 years ago

So, I'm making an RPG game, and my enemies sometimes break. They don't move at all! There are no errors or anything, they just stop working.

There's a few other things in my code, like the exp on death and stuff, but here's the loop:

while true do
    local low = script.Parent.Detect.Value
    local plr = nil

    --check distances
    for _, player in pairs(game.Players:GetPlayers()) do
        dis = player:DistanceFromCharacter(script.Parent.Head.Position)
        if dis <= low then
            plr = player
            low = dis --as soon as I added this line the code started breaking
        end
    end

    wait(.01)
    -- if there is a player in range, move to it
    if plr and plr.Character then
        repeat

            if plr.Character:FindFirstChild("HumanoidRootPart") then move(plr.Character.HumanoidRootPart) end
            if plr.Character.Head.Position.Y >= script.Parent.Head.Position.Y + 1 then script.Parent.Humanoid.Jump = true end
                wait(.01)
        until plr:DistanceFromCharacter(script.Parent.Head.Position) >= script.Parent.Detect.Value or not plr.Character:FindFirstChild("HumanoidRootPart")
        if plr.Character:FindFirstChild("HumanoidRootPart") then move(plr.Character.HumanoidRootPart) end
    end
end

Oh, and I should probably add the move function here too:

function move(part)
    if part then
        local to = part.Position
        local p = part.Parent[part.Name]
        script.Parent.Humanoid:MoveTo(to,p)
    end
end

please help

Answer this question