Help playing animation on letter click

Hi!

I have a script that works with tools in Workspace for playing animations (they don’t work if in server storage) so I decided it would probably be best to make it a key-pressed kind of thing. However, my miniscule pea brain has no idea how to make a keyboard key trigger an animation. Here’s my tool script (DOES NOT WORK IN SERVER STORAGE)

local plr = game.Players.LocalPlayer
local anims = {script.Parent.Hold, script.Parent.Swing}
local loadedAnims = {}
local tool = script.Parent
local animator
local debounce = true
local del = 2

tool.Equipped:Connect(function()
	script.Parent.Handle.Transparency = 1
end)

tool.Equipped:Connect(function(mouse) --On tool equipped do:
	animator = plr.Character:WaitForChild("Humanoid"):WaitForChild("Animator") --Find the animator object
	if not loadedAnims[1] then --If the animation wasn't loaded before
		loadedAnims[1] = animator:LoadAnimation(anims[1]) --Load it
	end
	loadedAnims[1]:Play()	--Play it
	
end)

--Click animation:
tool.Activated:Connect(function()
	if(debounce) then --Setting up the debounce (cooldown)
		debounce = false
		animator = plr.Character:WaitForChild("Humanoid"):WaitForChild("Animator") --Find the animator object
		if(not loadedAnims[2]) then --If the animation didn't exist yet
			loadedAnims[2] = animator:LoadAnimation(anims[2]) --Load it
		end
		loadedAnims[2]:Play() --Play it
		
		wait(del) --Wait the cooldown
		debounce = true
	end
end)

tool.Unequipped:Connect(function()
	loadedAnims[1]:Stop() 
	if(loadedAnims[2]) then 
		loadedAnims[2]:Stop()
	end
end)

Any help is appreciated, thanks!

1 Like

To make a keybind run something, You will need 2 things, userinput service, and a keybind. As shown below:

local YourKeybind = "A" --/ Change And Put The Keybind You Want To Use
game:GetService("UserInputService").InputBegan:Connect(function(Input, Other) --/ 'Other' Detects If Its Being Used When Using Chat Or Something Else.
if not Other and Input.Keycode == Enum.Keycode[YourKebind] then
if debounce then
debounce = false
animator = plr.Character:WaitForChild("Humanoid"):WaitForChild("Animator")
if not loadedAnims[1] then
loadedAnims[1] = animator:LoadAnimation(anims[1])
end
loadedAnims[1]:Play()
wait(del)
debounce = true
end
end
end)

You can also remove the ‘YourKeybind’ line and replace the [YourKeybind] on line 3 with the keybind after a period. (If its a symbol, you need the name of it)

Do you know where to place the script?

Here is an example of how you could do it Using UIS (UserInputService)

This should be in a Local script to or else it will not work.

local UIS = game:GetService("UserInputService")


UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then -- change e to your key then
		-- do the anim
	end
end)


UIS.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then -- change E to your key
		-- stop anim
	end
end)

So if you hold down the key the animation plays? If so, I only want the animation to play once when the player presses the key once, so what do I do?

I provided you the code above just change the E to your key
US.InputBegan detects when the player presses a Key
We check if the key is E then we play the animation

UIS.InputEnded detects when the player stops pressing a Key
We check if the key is E then we stop the animation, If you want the animation to play once a player presses a key (It’s currently E, change E to your key) then here is the code

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then -- change e to your key then
		Animation:Play()
	end
end)

I modified the script to this:

local UIS = game:GetService("UserInputService")
local loadedAnims = {}
local anims = {game.ReplicatedStorage.Swing}

local debounce = true
local del = 10

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.V then -- change e to your key then
		if(debounce) then --Setting up the debounce (cooldown)
			debounce = false
			game.Players.PlayerAdded:Connect(function(plr)
				local animator = plr.Character:WaitForChild("Humanoid"):WaitForChild("Animator")
				if(not loadedAnims[1]) then --If the animation didn't exist yet
					loadedAnims[1] = animator:LoadAnimation(anims[2]) --Load it
				end
				loadedAnims[1]:Play() --Play it

				wait(del) --Wait the cooldown
				debounce = true
			end)
		end
	end
end)

however it does not work. Is there any change I can make?

Is this a ServerScript?
If so you can’t use UIS (UserInPutService) Inside of a ServerScript you need a Local script.

I used a ServerScript in ServerScriptService.

That’s wrong you have to use Local script, It’s not possible with a ServerScript.
To be logical
The player has to press a Key on his client (Computer)
Not on the server…

Where do I put the script, then?

StarterPlayerScripts…
and add a Local script with this code

local UIS =game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.V then -- change e to your key then
		Animation:Play()
	end
end)

I added the variable, it still doesn’t work?

local Animation = game.ReplicatedStorage.Swing
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.V then
		Animation:Play()
	end
end)

This script is wrong and add the Animation in the script.

local Player = game.Players.LocalPlayer
local Animation = script.Animation
local PlayAnimation =  Player.Character:FindFirstChild("Humanoid"):LoadAnimation(Animation)
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.V then
		PlayAnimation:Play()
	end
end)

I changed this to

local Animation = script.Swing

but the script doesn’t work, I am going to try print debugging.

Edit: When I tried print debugging the item does not print.

Which one didn’t work???
Can I see your code?

I already fixed the script by looking it up on YouTube, thanks.

1 Like