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

Why is my animation making the game crash?

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)

1 comment

0

Which script did you use?

1 answer

✓ Accepted answer

"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)

1 comment

0

Thanks! that fixed the crashing but for some reason the animation still won't run, any idea why?

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