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
7 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.

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)

1
Don't use KeyDown. User#11440 120 — 7y
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 — 7y

2 answers

Log in to vote
0
Answered by 7 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 7 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.

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)


Answer this question