Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Key down Animation won't work?

As mentioned on my title, I've read through all the pages in wiki on Animations so I believe there should be no errors but yet it doesn't work, I even watched about 5 video tutorials from highly recommended Roblox channels and yet I still can't identify any errors especially since there's nothing in the output. Here's the script:

repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()  
Mouse.KeyDown:connect(function(Key)
    if Key == "q" then
        local Animation = Instance.new("Animation")
        Animation.AnimationId = "http://www.roblox.com/Asset?ID=281476213"
        local animTrack = Player.Character.Humanoid:LoadAnimation(Animation, Player.Character)
        animTrack:Play()
    end
end)

Thanks for the Help!

1 answer

KeyDown is deprecated

One problem is that KeyDown is deprecated, so don't use it.

Use proper events

Second, you are using KeyDown event of mouse, which does not exist. You can use Button1Down and Button1Up events for checking for clicks. For more information, check:

http://wiki.roblox.com/index.php?title=API:Class/Mouse

User Input Service

What I suggest you to use, though, is UserInputService, widely known as UIS.

First, you need to get the service:

UIS = game:GetService('UserInputService')

Then, you need to bind an event that fires when input has began and/or ended, depending on what you want to achieve.

UIS.InputBegan:connect(function(input,processed)
	if not processed then
		-- code
	end
end)

UIS.InputEnded:connect(function(input,processed)
	if not processed then
		-- code
	end
end)

When processed is false, it means that you can almost certainly assume that the input is intentional. Otherwise, you would capture input while someone is typing in chat and similar, which you usually don't want.

Next thing you have to do is check if key inputted is your desired key.

if input.KeyCode = Enum.KeyCode.Q then
	-- code
end

KeyCode variable holds the key that has been inputted (either started pressing on InputBegan event or released press on InputEnded event).

Here are all KeyCode Enums:

http://wiki.roblox.com/index.php?title=API:Enum/KeyCode

Now, you are set to do whatever you want when desired input is present. For more information about UIS, check out:

http://wiki.roblox.com/?title=API:Class/UserInputService

One more thing, I am not sure, but I think you should use rbxassetid:// instead of http://www.roblox.com/Asset?ID=ID. If that's not the case, you can revert the change I made.

Putting this all together for your code:

repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character
local Player = game.Players.LocalPlayer

UIS = game:GetService('UserInputService')

UIS.InputBegan:connect(function(input,processed)
	if not processed then
		if input.KeyCode == Enum.KeyCode.Q then
			local Animation = Instance.new("Animation")
	        Animation.AnimationId = "rbxassetid://281476213"
	        local animTrack = Player.Character.Humanoid:LoadAnimation(Animation, Player.Character)
	        animTrack:Play()
		end
	end
end)

Hope this helped.

3 comments

0

Didn't Work

0

Just saying it didn't work does not help me debug my code and help you further. Did you get any errors? If not, what did this script not do that you wanted it to do?

0

I see I have a syntax error at line 8, I edited my code. Although, I am not sure if your animation will load, so test it and see if you get 'Animation failed to load' or something.

This discussion is preserved as it was. New answers, comments, and votes are closed.