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

Why isin't my keypress animation playing?




local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent
local AnimationID = script.Parent.LocalScript:GetChildren()
local Humanoid = Character.Humanoid
local UserInputService = game:GetService("UserInputService")
local Debounce = true


if #AnimationID >= 0 then
	
for _, v in pairs (AnimationID) do
	
local Animation = v

UserInputService.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Enum.KeyCode["E"] and Debounce == true then
		local LoadAnimation = Humanoid:LoadAnimation(Animation)
		LoadAnimation:Play()
		wait(1)
	Debounce = true
	
			else
		return
			end
		end)
	end
end

The animation is bought from a store and it loads in properly into the local script, however when I press e nothing happens and I get no errors

2 comments

0

Try changing the priority of the animation in the animation editor. Open the animation editor, import the animation, and click on the three dots in the animation editor, and hover over change priority. Change it to action, and export the animation to the website, updating the older version. Test it again and see what happens.

0

Its already set to action

2 answers

It's only checking if animations is greater than 0 once, hence the inputbegan not working everytime. Try this

local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent
local AnimationID = script.Parent.LocalScript:GetChildren()
local Humanoid = Character.Humanoid
local UserInputService = game:GetService("UserInputService")
local Debounce = true

UserInputService.InputBegan:Connect(function(Input, IsTyping)
	if #AnimationID > 0 then
	 	for _,v in pairs(AnimationID) do
		    local Animation = v
		    if IsTyping then return end
			    if Input.KeyCode == Enum.KeyCode["E"] and Debounce == true then
		        local LoadAnimation = Humanoid:LoadAnimation(Animation)
		        LoadAnimation:Play()
		        wait(1)
		    	Debounce = true
	               end
		end
	end
end)

1 comment

0

Didn't work

Ok, there are quite a few problems.

local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent
local AnimationID = script.Parent.LocalScript:GetChildren()
local Humanoid = Character.Humanoid
local UserInputService = game:GetService("UserInputService")
local Debounce = true


if #AnimationID >= 0 then

for _, v in pairs (AnimationID) do
	local Animation = v
		UserInputService.InputBegan:Connect(function(Input, IsTyping)
			if IsTyping then return end
			if Input.KeyCode == Enum.KeyCode["E"] and Debounce == true then
				local LoadAnimation = Humanoid:LoadAnimation(Animation)
				LoadAnimation:Play()
				wait(1)
				Debounce = true
			else
				return
			end
		end)
	end
end

I fixed the indentation.

Looking at your code, there's one thing that I noticed.

You set debounce's initial value to true, when it is conventional to set it to false. I normally wouldn't point out an unconventional practice, but in this instance, you don't change its value at all. You check to see if it's true and then you don't immediately change it to false. You wait 1 second and set it back to true. What you should be doing is, after confirming that it is one state, immediately set it to the other state, wait 1 second, and then set it back to the first state.

Because I'm resolute when it comes to conventional practices, I'll do this:

local Debounce = false

if Input.KeyCode == Enum.KeyCode.["E"] and Debounce == false then
	Debounce = true
	local LoadAnimation = Humanoid:LoadAnimation(Animation)
	LoadAnimation:Play()
	wait(1)
	Debounce = false
	else
		return
	end
end)

Also, you don't have to do Enum.KeyCode["E"]. You could just put Enum.KeyCode.E. Using the brackets and quotation marks is really only necessary if the name of the KeyCode has a space or a special character or a number at the start.

Now, it should work properly, or at least in the aspect of preventing the animation from playing too many times.

I noticed that you said the animation didn't play at all. Your previous code would work, only there would be no limit as to how often you could play the animation. The updated code should fix that in that regard.

I believe the reason the animation isn't playing is because of its priority.

Go into Roblox Studio, and go to the Plugins tab. Click the plugin that says "Build Rig". Create either a R15 or R6 rig(depending on the animation), and open the Animation Editor plugin. Click on the three dots in the top left corner of the Animation Editor panel, and import the animation. It should be in your inventory if you own it. Import the animation, and click on the three dots, and hover over "Animation Priority." Set it to action. Then, export the animation and update the older version of the animation.

I think this should fix the issues you have. If someone would like to correct my answer, if it's wrong(because I'm an idiot and can't spot my own errors, if any), then make sure to comment and correct any of my faults.

2 comments

0

Didn't work either

0

Interesting. Animations are always a pain.

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