#Hello, i am trying to make my animation a toggle animation but i cant not get it to work please help

1 messages · Page 1 of 1 (latest)

keen canopy
#

local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://125317000639249"
local AnimationTrack

local function getHumanoid()
local character = Player.Character or Player.CharacterAdded:Wait()
return character:FindFirstChildOfClass("Humanoid")
end

local function ensureAnimationTrack()
if not AnimationTrack or not AnimationTrack.Parent then
local humanoid = getHumanoid()
if humanoid then
AnimationTrack = humanoid:LoadAnimation(Animation)
end
end
end

UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.C then
ensureAnimationTrack()
if AnimationTrack then
if AnimationTrack.IsPlaying then
AnimationTrack:Stop()
else
AnimationTrack:Play()
end
end
end
end)

-- Recreate AnimationTrack if character respawns
Player.CharacterAdded:Connect(function()
AnimationTrack = nil
end)

this is the code i got so far, i used a little bit ai after i got stuck.

sage spear
#

What is not working

keen canopy
#

its not toggle

sage spear
#

Wdym bro

wary tide
#

Cus youre loading animation from humanoid

sage spear
#

Also

mental arrowBOT
#

studio** You are now Level 17! **studio

sage spear
#

Its because you're making constatly a new animation track

sage spear
#

He said it doesnt toggle

#

I can assume that animation plays perfeclty fine, but doesnt turn off

#

Everytime you click a key u make a new animation track, theofore when you check if track plays it will actually return it doesnt

#

So it will Play it again

keen canopy
#

I dont wnt it to reset

sage spear
#

Huh

faint condor
keen canopy
#

I want it to end on sitting down.

sage spear
#

Wha

faint condor
#

huh

#

“sit down after animation ends”

#

😂

keen canopy
#

Basically the animation makes it the player sit down, so i want when you press "c" once it keeps the player sat down and press "c" again it stands up.

#

🙂

sage spear
#

So u have to keep track of currently played animations on your character...

#

As i said...

#

You're making every time you click a new animation track

#

It doesnt matter if animation is the same, a new track means new track

#

Simple as that

#

You must get all currently played animations on your character and get one u want to stop

keen canopy
#

Okay, i will try. Thanks!

sage spear
#

Btw

#

Use Animator instaed of humanoid

#

It has funny method that returns currently played tracks

#

My mistake

#

Humanoid also have it