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

How do I get my animation to play when I click an object and stop when I click it again?

local Flourbag = game.Workspace.Flourbag
local clickDec = game.Workspace.Flourbag.ClickDetector
local Animation = Instance.new("Animation")
Animation.Parent = game.Workspace
Animation.AnimationId = "http://www.roblox.com/asset/?id=5446725528" 
local punch = 1

clickDec.MouseClick:Connect(function(Player)
	if punch == 1 then
		local animTrack = Player.Character.Humanoid:LoadAnimation(Animation)
		animTrack:Play() 
		punch = 2
	elseif punch == 2 then
		animTrack:Stop() --animTrack is underlined in red for some reason
		punch = 1
	end
end)

I have the script parented to the flourbag. The animation plays when I click the flourbag but when I click again it doesn't stop. Sorry if my question is stupid as I'm very new to scripting.

1 answer

✓ Accepted answer

Your script is not working because you're defining the local variable at a lower scope than the needed, you need to set it globally to the script, however you're setting it inside the if what makes it just be defined inside it.

Also, remember your script isn't a local script(i belive) so it would re-write the variable everytime someone clicked the part, so you need to bypass that. In this case I used a table that stores each players animation track, so we can easily stop it

local Flourbag = game.Workspace.Flourbag
local clickDec = game.Workspace.Flourbag.ClickDetector
local Animation = Instance.new("Animation")
Animation.Parent = game.Workspace
Animation.AnimationId = "http://www.roblox.com/asset/?id=5446725528" 
local animTracks = {} -- Here we define a table, that will store the animation for each player

clickDec.MouseClick:Connect(function(Player)
    if not animTracks[Player.Name] then --Here we detect  if player currently don't have a animtrack set...
        animTracks[Player.Name] = Player.Character.Humanoid:LoadAnimation(Animation)
        animTracks[Player.Name]:Play() 
    else
        animTracks[Player.Name]:Stop() --Stop the animation from running
        animTracks[Player.Name] = nil -- Undefine player animtrack, so we can play the animation again
    end
end)


If you want more information about scopes, here it is:

https://developer.roblox.com/en-us/articles/Scope

if you want more information about tables(and dictionaries), here it is

https://developer.roblox.com/en-us/articles/Table

If this answers your question, please mark it as accepted :)

5 comments

0

I'm a little confused, what do I do with the "punch == 2"? Right now nothing works in the script.

0

oh yes, I totally forgot about it, just remove that if i'll edit the answr

0

It still doesn't work, it says "attempt to call a nil value" (sorry for the inconvenience)

0

I edited it again, sorry for the error, I wasn't paying attention when I edited it first time

0

Thank you!

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