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)

2 comments
Don't use KeyDown.
Every key down you create the animation object and load it to the players humanoid not good, this only needs to load the animetion every time they spawn :/