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

Animation plays multiple times then stops?

Asked by 4 years ago
Edited 4 years ago

I have an AI character that is supposed to walk toward you, and play an animation. It does this, but just spams the first few frames of said animation, and then stops.

anim = Instance.new("Animation",script.Parent.Parent.Humanoid)
anim.AnimationId = "http://www.roblox.com/asset/?id=5063671356"
humanoid = script.Parent.Parent.Humanoid

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                local playAnim = humanoid:LoadAnimation(anim)
                      playAnim:play()                  
        end
end
script.Parent.Touched:connect(onTouched)

any help?

EDIT: This script is located in the models upper torso if that is any help.

0
did you set the animation priority to action? ProjectInfiniti 192 — 4y
0
I'm very new to animation and coding in general. but how would I do that? Dreadd15 4 — 4y
0
please use :Connect() and not :connect() WideSteal321 773 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The most common problem with Touched functions is that they run more than once. Add a debounce variable that stops it from running more than once in a certain time.

anim = Instance.new("Animation",script.Parent.Parent.Humanoid)
anim.AnimationId = "http://www.roblox.com/asset/?id=5063671356"
humanoid = script.Parent.Parent.Humanoid

local debounce = false

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) and debounce == false then
        debounce = true
                local playAnim = humanoid:LoadAnimation(anim)
                      playAnim:play()
            playAnim.Stopped:Wait() -- you should change the delay, but this means that the next line doesn't run until the animation stops.
            debounce = false                  
        end
end
script.Parent.Touched:connect(onTouched)

Is the NPC doing anything else?

0
Thank you! This works! Dreadd15 4 — 4y
Ad

Answer this question