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.
01 | local player = game.Players.LocalPlayer |
02 | local mouse = player:GetMouse() |
03 | dance = false |
04 | function keyD(key) |
05 | local key = key:lower() |
06 | local hotkey = script.Hotkey.Value |
07 | local dance = Instance.new( "Animation" ) |
08 | dance.AnimationId = "rbxassetid://482114411" -- you'll need to use a different animation if you want to test this script |
09 | local animloader = player.Character.Humanoid:LoadAnimation(dance) |
10 | if key = = hotkey then |
11 | if dance = = false then |
12 | animloader:Play() |
13 | dance = true |
14 | else |
15 | animloader:Stop() |
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.
01 | local player = game.Players.LocalPlayer |
02 | local mouse = player:GetMouse() |
03 | local dancing = false -- Declaring that when you are loading, it's starting as false. Not in func for obvious reasons. |
04 | function keyD(key) |
05 | local key = key:lower() |
06 | local hotkey = script.Hotkey.Value |
07 | local dance = Instance.new( "Animation" ) |
08 | dance.AnimationId = "rbxassetid://482114411" -- you'll need to use a different animation if you want to test this script |
09 | local animloader = player.Character.Humanoid:LoadAnimation(dance) |
10 | if key = = hotkey then |
11 | if dancing then |
12 | animloader:Play() |
13 | dancing = true |
14 | 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. |
15 | animloader:Stop() |