Everytime I touch the torso it crashes. Any idea what I'm doing wrong?
local animation = Instance.new("Animation") animation.AnimationId = "http://www.roblox.com/Asset?ID=94161088" function anim() local animController = Instance.new("AnimationController") local animTrack = animController:LoadAnimation(animation) animTrack:Play() end script.Parent.Torso.Touched:connect(anim)
"Touch" is a touchy event, because it's possible to execute numerous functions in one touch. To reduce the function executions, implement a debounce, which executes the function one at a time.
Animation = Instance.new("Animation", script.Parent) -- I love my animation objects to exist Animation.AnimationId = "http://www.roblox.com/asset/?id=94161088" Debounce = false function Anim() if not Debounce then Debounce = true local AnimController = Instance.new("AnimationController", script.Parent) local AnimTrack = AnimController:LoadAnimation(Animation) AnimTrack:Play() wait(2) -- Or however long your animation is, or however you want it to be Debounce = false end end script.Parent.Torso.Touched:connect(Anim)
It's quite odd that a new AnimationController object must be made every time the function runs, as it essentially replaces the Humanoid in the role of animations. So instead of creating a new one every time, just insert it manually in studio to the parent of this script.
AnimController = script.Parent.AnimationController Animation = Instance.new("Animation", script.Parent) Animation.AnimationId = "http://www.roblox.com/asset/?id=94161088" AnimTrack = AnimController:LoadAnimation(Animation) Debounce = false function Anim() if not Debounce then Debounce = true AnimTrack:Play() wait(2) -- Or however long your animation is, or however you want it to be Debounce = false end end script.Parent.Torso.Touched:connect(Anim)