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

Does anyone know the proper way to do this? Mousedown or key down for animation.

Asked by 7 years ago

Okay so I'm sure you can tell by reading the code but if not I'm trying to play an animation when you press MouseButton1 or a certain key. I'm going to post all of the code I've tried and I would appreciate it some help correcting it. The first one is my most recent attempt which gave me an error of "UserInputService is not a valid member of InputObject" which I'm assuming means I can't use userinputservice for keydown. The only reason I'm using userinputservice is because this YouTube tutorial on how to do this wasn't working so I posted it and they suggested it. So I don't know maybe I'm doing all of this wrong.

--first way--
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local function onInputBegan(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local keyPressed = input.KeyCode
    end 

local Animation = script:WaitForChild("Animation") 
    if input.UserInputService == "f" or "F" then
        Animation:Play()
        end 
end
UserInputService.InputBegan:connect(onInputBegan)
--second way--
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
game:GetService('UserInputService')
animation = script:WaitForChild("Animation")
enabled = false
mouse.Button1Down:connect(function()
    if enabled ==false
        enabled = true
        local animationTrack = player.Character.Humanoid:LoadAnimation(animation)
        animationTrack:Play()

        wait(3)
    end
end)
--3rd way--
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
animation = script:WaitForChild("Animation")
enabled = true

local function onInputBegan(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
            repeat wait() until player.Character.Humanoid == player.Character.Humanoid:LoadAnimation(animation)

    end
end
UserInputService.InputBegan:connect(onInputBegan)
--4th way--
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
animation = script:WaitForChild("Animation")
enabled = true

local function onInputBegan(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
            local animationTrack = player.Character.Humanoid:LoadAnimation(animation)
            animationTrack:Play()
            wait(1)
            enabled = true
            animationTrack:Play()
    end
end
UserInputService.InputBegan:connect(onInputBegan)

1 answer

Log in to vote
1
Answered by 7 years ago

I can try to help you, but I'm REALLY tired so I can't really analyze your script. Anyhow, I can show you the proper way to do it.

-- Local Script Inside StartPack/StarterGui
local plr = game.Players.LocalPlayer
local char = plr.Character
local animation = char.Humanoid:LoadAnimation(script:FindFirstChild("Animation")) -- Change "Animation" to whatever the name of your animation is and make it a child to the script
game:GetService("UserInputService").InputBegan:connect(function(input) -- UIS 
    if input.KeyCode == Enum.KeyCode.Q then -- when q is pressed, do stuff. (change q to whatever key you want)
        animation:Play()
    end
end)

If you don't understand anything or it doesn't work, let me know.

0
Also, use Connect instead of connect. Ultimate_Piccolo 201 — 7y
0
The parent and children stuff should look like this. https://gyazo.com/f5fd0328ce3c0dbd8995adf085575114 Ultimate_Piccolo 201 — 7y
0
Although don't mind the "Q" tool Ultimate_Piccolo 201 — 7y
Ad

Answer this question