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

Make NPC jump if moving slower than a certian speed?

Asked by 2 years ago

I'm using the stock enemy npc and related scripts by xBattleBear.

I've successfully tweaked the scripts to my liking a few times, and now I'm trying to make the npc jump if it's moving slower than it should be, to get over obstacles.

I have two scripts - one for jumping, and one for following.

I've written this jump script myself - It works for around 5 seconds, and then the npc just starts jumping into the sky.

script.Parent.Humanoid.Running:Connect(function(speed)
    while true do
        wait(2)
        if speed < 2 and #script.Parent.follow:GetChildren() > 0 then
            wait(2)
            script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
        end
    end
end)

And this is the following script I've worked to how I want. I have a part created so that the jumping script knows when to fire, because if I don't have some sort of check, the npc jumps like a madman.

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 50
    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) and (temp2.Name ~= "Enemy") then
            temp = temp2:findFirstChild("Torso")
            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
--wait(math.random(0,5)/10)
while true do
    wait(0.5)
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        identifier = Instance.new("Part")
        identifier.Anchored = true
        identifier.CanCollide = false
        identifier.Transparency = 1
        identifier.Parent = script
        script.Parent.Humanoid:MoveTo(target.Position, target)
        script.Parent.Humanoid.Part = nil
    end

end

I also tried putting a "speed < 10 and speed > 0" in place of speed < 2 and #script.Parent.follow:GetChildren() > 0, but then the npc won't jump if it isn't moving, which can be the case if a player uses a higher point correctly.

0
Make a check if its touching the ground before enabling the jump property. If you enable the jump property in midair, it jumps again in midair. NarwhalAndMe 141 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

Alright so let's analyze your script. On line 1, the function runs when the speed at which the NPC runs changes. But on line 2, the function triggers a while loop to change the humanoid's state when the speed changes. This is the problem here because once the while loop is triggered, it will stay there forever so the loop will continue to run infinitely which means the NPC will continue to jump even if it is not moving. To solve the problem, you can either remove the while loop or add a break after you change the humanoid's state.

script.Parent.Humanoid.Running:Connect(function(speed)
    while true do -- You can remove the loop
        wait(2)
        if speed < 2 and #script.Parent.follow:GetChildren() > 0 then
                    wait(2)
            script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
            -- Or add a break here
        end
    end
end)
0
This worked! Thank you very much! MonoBlitz 9 — 2y
Ad

Answer this question