How do i make a toggable animation?

Hey people,

Im currently making a roleplaying game where you can hang out, roleplay with the morphs etc.
The problem is that i have NO experience with any coding, so i cant do anything diffucult without needing a specific tutorial for it.

For my game i made this little side-tab that has animations (i made it using a tutorial)
Heres what it looks like:

C__Users_jilli_OneDrive_Documents_ROBLOX_RP game wowie.rbxl - Roblox Studio 1-3-2021 18_43_29

i used this code (in a local script btw) for making the buttons work:

local animation = Instance.new(“Animation”)
animation.AnimationId = “rbxassetid://6339288616”

local trackanimation = nil
local playability = true

function playAnimation (animationSource)
if playability == true then
local plr = game.Players.LocalPlayer
trackanimation = plr.Character.Humanoid:LoadAnimation (animation)

	trackanimation.KeyframeReached:Connect(function()
		print("animation Succesfully Played!")
		
	end)
	trackanimation:Play ()
end

end
script.Parent.MouseButton1Click:Connect(playAnimation)

With this code, i made the animation (which i didnt loop for reasons ill explain later) play then stop again when its done playing.

Now, heres my problem:
i want to add a “sit” animation, where your character sits down. Only thing is that for you character to keep sitting you have to loop the animation. But if i make the animation loop, the character will never stop sitting!

So, could somone please explain to me (in easy to follow steps since i dont know anything about coding) how i can change the code to where the animation plays when you click the button, but then stops again when you press the button a second time?

Thanks in advance! :]

4 Likes

please ignore the weird lookin script, i dont know why its like that. just in case your confused, everything below “I used this code for making the buttons work” thats the entire script :']

You can stop a looping animation by just using AnimationTrack:Stop()

Also, you should use a variable (add it to the script) using true/false (A boolvalue) and name it IsCharacterSitting (or anything as long as you’ll recognize it)

Make the variable change from true to false (or vice versa) whenever the gui is clicked.

Then use some if statements to check if the character is doing the animation or not, and then add the code to play/stop the animation.

thanks for the help, but the thing is that i want the animation to keep looping until the button is pressed a second time. (also could you explain the code in a bit more simpler way? im still a little confused)

If you’ve created a new sitting animation then you can replace the existing sitting animation in the player. Otherwise, you can just do Humanoid.Sit = true on the player’s humanoid.

To make sitting toggleable, try this:

local humanoid = player.Humanoid

function ToggleSit()
    if humanoid.Sit then
        humanoid.Sit = false
    else
        humanoid.Sit = true
    end
end
script.Parent.MouseButton1Click:Connect(ToggleSit)

The sit animation is inside the player’s Animation script. You can replace the ID on it with your own.

i dont think this will help with it… i kinda just want the character to play a animation where they look like they are sitting, but make it so the animation stops when you press the same button a second time.

oh, nvm i think the variable is for the button to be able to stop the animation, sorry!
(atleast i think so)

but i still dont know how to code that, could you show me an example?
sorry in advance, i really dont know what im doing

If you take your animation and give the last keyframe a good name, like “end” or something similar then when you play your animation, you can pause it right before it ends, keeping the player in the sitting position. Once they press the button again, you can unpause it.

local connection = nil
local isSitting = false

function ToggleSittingAnimation()
    if isSitting then
        isSitting = false
        connection:Disconnect()
        trackanimation:AdjustSpeed(1)
    else
        isSitting = true
        connection = trackanimation.KeyframeReached:Connect(function(keyframeName)
            if keyframeName == "end" then
                trackanimation:AdjustSpeed(0)
            end
        end)
        trackanimation:Play()
    end
end

script.Parent.MouseButton1Click:Connect(ToggleSittingAnimation)

Edit: Just fixed the formatting

oh, thats sounds easy, but how would i rename the keyframe? (i use the default roblox animator btw)

As far as I know you should be able to just go into the place where your animation is stored and physically rename the keyframe object.

ah okay, i will try it out right away! (do i copy your code and replace it with mine or do i paste it under it?)

You can copy and replace it directly if you want, but if variable names are different you’ll have to change them.

doesnt work, i get this error:

Players.Fluff_Daamazingx33.PlayerGui.Poses.Poses.ScrollingFrame.TextButton.LocalScript:11: attempt to index nil with ‘KeyframeReached’

Make sure your animation track for sitting is called ‘trackanimation’ and is available to the function.

It might help if I could see the rest of your script.

oh, ill try! (also i forgot something inportant when making the animation- it has to be an idle… ill fix it
)

also i think i forgot to make the animation loop…

Make sure you load the sit animation somewhere and then save the loaded animation as a variable in your script. Once it’s loaded and it’s called trackanimation then the code should work.

how do i save it as a variable? i have no idea how

why is this all so complicated for me?? i just want a looping animation to stop playing after you press a button twice :']