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

Why does this script make an NPC run only once?

Asked by 10 years ago
script.Parent.Humanoid.StateChanged:connect(function(state)
    if state == Enum.HumanoidStateType.Running then
        for i = 1, 6 do
            wait(0.01)
            rightarm.DesiredAngle = rightarm.DesiredAngle +0.05
            leftarm.DesiredAngle = rightarm.DesiredAngle -0.05
            rightleg.DesiredAngle = rightarm.DesiredAngle +0.05
            leftleg.DesiredAngle = rightarm.DesiredAngle -0.05
        end
        wait(0.02)
        for i = 1, 6 do
            wait(0.01)
            rightarm.DesiredAngle = rightarm.DesiredAngle -0.05
            leftarm.DesiredAngle = rightarm.DesiredAngle +0.05
            rightleg.DesiredAngle = rightarm.DesiredAngle -0.05
            leftleg.DesiredAngle = rightarm.DesiredAngle +0.05
        end
        wait(0.02)
        for i = 1, 6 do
            wait(0.01)
            rightarm.DesiredAngle = rightarm.DesiredAngle -0.05
            leftarm.DesiredAngle = rightarm.DesiredAngle +0.05
            rightleg.DesiredAngle = rightarm.DesiredAngle -0.05
            leftleg.DesiredAngle = rightarm.DesiredAngle +0.05
        end
        wait(0.02)
        for i = 1, 6 do
            wait(0.01)
            rightarm.DesiredAngle = rightarm.DesiredAngle +0.05
            leftarm.DesiredAngle = rightarm.DesiredAngle -0.05
            rightleg.DesiredAngle = rightarm.DesiredAngle +0.05
            leftleg.DesiredAngle = rightarm.DesiredAngle -0.05
        end
        wait(0.02)
    end
end)

I want to make it so that an NPC runs until the state has changed but this only makes it run once, how do I change it so that it runs until the state has changed?

0
Hello? CharcterKaiser 0 — 10y

1 answer

Log in to vote
0
Answered by 10 years ago
--Er so I'm not sure about this and I'm at school so I cannot test it but if it doesn't work try giving me output

local running = false

coroutine.resume(coroutine.create(function()
    repeat wait(0.1) until running == true
    while running do
        for i = 1, 6 do
            wait(0.01)
            rightarm.DesiredAngle = rightarm.DesiredAngle +0.05
            leftarm.DesiredAngle = rightarm.DesiredAngle -0.05
            rightleg.DesiredAngle = rightarm.DesiredAngle +0.05
            leftleg.DesiredAngle = rightarm.DesiredAngle -0.05
        end
        wait(0.02)
        for i = 1, 6 do
            wait(0.01)
            rightarm.DesiredAngle = rightarm.DesiredAngle -0.05
            leftarm.DesiredAngle = rightarm.DesiredAngle +0.05
            rightleg.DesiredAngle = rightarm.DesiredAngle -0.05
            leftleg.DesiredAngle = rightarm.DesiredAngle +0.05
        end
        wait(0.02)
        for i = 1, 6 do
            wait(0.01)
            rightarm.DesiredAngle = rightarm.DesiredAngle -0.05
            leftarm.DesiredAngle = rightarm.DesiredAngle +0.05
            rightleg.DesiredAngle = rightarm.DesiredAngle -0.05
            leftleg.DesiredAngle = rightarm.DesiredAngle +0.05
        end
        wait(0.02)
        for i = 1, 6 do
            wait(0.01)
            rightarm.DesiredAngle = rightarm.DesiredAngle +0.05
            leftarm.DesiredAngle = rightarm.DesiredAngle -0.05
            rightleg.DesiredAngle = rightarm.DesiredAngle +0.05
            leftleg.DesiredAngle = rightarm.DesiredAngle -0.05
        end
        wait(0.02)
    end
end))

script.Parent.Humanoid.StateChanged:connect(function(state)
    if state == Enum.HumanoidStateType.Running then
        running = true
    else
        running = false
    end
end)

If this doesn't work try to give output or fix up what I'm forgetting. I have no way of testing this as of now so tell me how it works out!

0
It didn't work, there was no error or nothing, the legs and arms didn't animate. CharcterKaiser 0 — 10y
Ad

Answer this question