So when the player press X, the script checks if dance = false, if so, the player will perform a dance animation. If the player press X again, the player should stop dancing. However, the player won't stop dancing if they press X while performing the dance animation. The dance animation type is Action and is looped.
local player = game.Players.LocalPlayer local mouse = player:GetMouse() dance = false function keyD(key) local key = key:lower() local hotkey = script.Hotkey.Value local dance = Instance.new("Animation") dance.AnimationId = "rbxassetid://482114411" -- you'll need to use a different animation if you want to test this script local animloader = player.Character.Humanoid:LoadAnimation(dance) if key == hotkey then if dance == false then animloader:Play() dance = true else animloader:Stop() dance = false end end end mouse.KeyDown:connect(keyD)
Unfortunately from what I learned that if an animation is looped it won't stop after being played. Sorry.
You can stop an animation after being played, so just with a quick edit, you're on your way.
I'll highlight my changes with comments in the script.
local player = game.Players.LocalPlayer local mouse = player:GetMouse() local dancing = false -- Declaring that when you are loading, it's starting as false. Not in func for obvious reasons. function keyD(key) local key = key:lower() local hotkey = script.Hotkey.Value local dance = Instance.new("Animation") dance.AnimationId = "rbxassetid://482114411" -- you'll need to use a different animation if you want to test this script local animloader = player.Character.Humanoid:LoadAnimation(dance) if key == hotkey then if dancing then animloader:Play() dancing = true elseif not dancing then -- You don't need much of anything after this, since if it's not the key, you don't need to stop anything. Instead, you just have another conditional statement checking its state. animloader:Stop() dancing = false end end end mouse.KeyDown:connect(keyD)