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

Why is my animation making the game crash?

Asked by
Drogb4 15
9 years ago

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)
0
Which script did you use? Redbullusa 1580 — 9y

1 answer

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

"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)
0
Thanks! that fixed the crashing but for some reason the animation still won't run, any idea why? Drogb4 15 — 9y
Ad

Answer this question