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

How do I trigger animation when i click a button on the keyboard?

Here's the script:

wait(2)

local player = game.Players.LocalPlayer local Mouse = player:GetMouse() Anim = script.AthleticSplits

Mouse.KeyDown:connect(function(key) if key == "q" then s = player.Character.Humanoid:LoadAnimation(Anim) s:Play() end end)

1 answer

I see the problem, KeyDown is deprecated. I advise you to use UserInputService. Here is the script, I will also explain it below. MAKE SURE THIS IS IN A LOCALSCRIPT!!

repeat wait() until game.Players.LocalPlayer.Character ~= nil

local player = game.Players.LocalPlayer
local char = player.Character
local InputService = game:GetService("UserInputService")

InputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		local animation = script.AthleticSplits
		local anime = char:WaitForChild("Humanoid"):LoadAnimation(animation)
		anime:Play()
	end
end)

We will be using a service called, UserInputService. It's much like KeyDown but kinda more advanced. KeyCode just means the key a user/you will press, like Q or E. Now we are defining the animation as the AthleticSplits and defining anime to load that animation. And then we play it!

The repeat wait() until game.Players.LocalPlayer.Character ~= nil just means that the script will wait for the character to load so the script can work properly.

Hope this helped!

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