Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why isn't my keyDown script for a looped animation working?

Asked by
Seraine 103
8 years ago

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.

01local player = game.Players.LocalPlayer
02local mouse = player:GetMouse()
03dance = false
04function 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()  
View all 21 lines...
1
Don't use KeyDown. User#11440 120 — 8y
0
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 :/ User#5423 17 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

Unfortunately from what I learned that if an animation is looped it won't stop after being played. Sorry.

Ad
Log in to vote
0
Answered by 8 years ago

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.

01local player = game.Players.LocalPlayer
02local mouse = player:GetMouse()
03local dancing = false -- Declaring that when you are loading, it's starting as false. Not in func for obvious reasons.
04function 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()
View all 21 lines...

Answer this question