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

Animation on key press isn't working, can someone help? (me and my team are fairly new to Lua)

Asked by
Mekski 0
3 years ago

I am making an ability based game and I have an ability that when I press Q I gain movement speed and become semi-transparent. We (this is a group game using team create) made an animation for activating the ability, but it isn't working. Here is the LocalScript,

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()

    local debounce = false

    local anim = Instance.new("Animation")
    anim.AnimationId = "http://www.roblox.com/asset/?id=6271298217"

    mouse.KeyDown:connect(function(key)
        if key == "q" then
         local playAnim = humanoid:LoadAnimation(anim)


                if not debounce then

             debounce = true

               playAnim:Play()

                wait(6)

                debounce = false

          end
       end
    end)

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

You should refer to official documentation before watching any programming tutorials; check the dates to ensure you're not learning outdated practices.

The .KeyDown Signal of Mouse was deprecated ages back, it's recommended you use its successor, UserInputService.

Note: I suggest you create the Animation Instance beforehand and modify the respective properties to reduce unnecessary operations.

--###----------[[SERVICES]]----------###--
local UserInputService = game:GetService("UserInputService")


local Players = game:GetService("Players")



--###----------[[VARIABLES]]----------###--
local Player = Players.LocalPlayer


local Character = Player.Character or Player.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild("Humanoid")


local SpeedBoostKeystroke = Enum.KeyCode.Q


local AbilityAnimation = script.AbilityAnimation --//


local CanActivate = true



--###----------[[FUNCTIONS]]----------###--
local function OnInputBegan(InputObject, GameProcessedInput)
    if GameProcessedInput or not CanActivate then return end
    ---------------
    if InputObject.KeyCode == SpeedBoostKeystroke then
        ---------------
        CanActivate = false
        ---------------
        Humanoid.Animator:LoadAnimation(AbilityAnimation):Play()
        ---------------
        wait(6)
        ---------------
        CanActivate = true
    end
end



--###----------[[SETUP]]----------###--
UserInputService.InputBegan:Connect(OnInputBegan)
Ad

Answer this question