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

How to make an animation play for as long as I hold a button?

Asked by
bluzorro 417 Moderation Voter
4 years ago
Edited 4 years ago

I'm making a charging animation where the player recharges their energy. However, after a certain amount of time of holding, the animation stops.

-- Client

local UIS = game:GetService("UserInputService")
local debounce = false

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local database = player:WaitForChild("Database")
local magicType = database.Magic

local charge = Instance.new("Animation")
charge.AnimationId = "rbxassetid://4738576556"
local chargeAnim = hum:LoadAnimation(charge)

local isHolding = false

chargeAnim.KeyframeReached:Connect(function(keyframe)
    if keyframe == "Pause" then
        chargeAnim:AdjustSpeed(0)
        chargeAnim.TimePosition = chargeAnim.Length
    end
end)

UIS.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.LeftShift and not debounce and not isHolding then
        debounce = true
        isHolding = true

        chargeAnim:Play()
        hum.WalkSpeed = 0
        hum.JumpPower = 0
        wait()
        debounce = false
    end
end)

UIS.InputEnded:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.LeftShift and not debounce and isHolding then
        debounce = true
        isHolding = false

        chargeAnim:Stop()
        hum.WalkSpeed = 16
        hum.JumpPower = 50

        wait(0.5)
        debounce = false
    end
end)

I've even tried freezing the animation at a point, but it still stops the animation.

0
I don't get why this doesn't work... MrLonely1221 701 — 4y
0
neither do i.. bluzorro 417 — 4y

Answer this question