Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Animation plays multiple times then stops?

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.

3 comments

0

did you set the animation priority to action?

0

I'm very new to animation and coding in general. but how would I do that?

0

please use :Connect() and not :connect()

1 answer

✓ Accepted answer

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?

1 comment

0

Thank you! This works!

This discussion is preserved as it was. New answers, comments, and votes are closed.