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

How to properly trigger an animation when a key is held down?

Pretty new at scripting, I managed to dig up a script that's similar to what I want here and tweaked it a bit. Basically, I was trying to make a script that triggers a blocking animation when holding down the "E" key. This is the LocalScript I used titled "BlockScript".

local p = game.Players.LocalPlayer
local anim = script.BlockAnim
local serv = game:GetService("UserInputService")
local char = p.Character
if not p.Character then
    repeat wait() until p.Character
end
local human = char:WaitForChild("Humanoid")

serv.InputBegan:connect(function(key,processed)
    if key.KeyCode == Enum.KeyCode.E and processed == true then

	    print("BlockAnim")
	    local newAnim = human:LoadAnimation(anim)
	    newAnim:Play()
    end
end)

I then attached the blocking animation titled "BlockAnim" to the LocalScript. enter image description here However, when I insert the script into StarterCharacterScripts, it tells me that "BlockAnim is not a valid member of LocalScript." I also tried putting the BlockScript into StarterPlayerScripts and StarterPack, but neither work either. I'd appreciate the help!

2 comments

0

idk if this will work but try do if script:FindFirstChild("BlockAnim") then local anim = script.BlockAnim

0

Alright, that worked for getting rid of the error! However, the animation still seems to not trigger/play. It doesn't give me the print that BlockAnim is playing in the output either. In the script, the "anim" in line 14 is underlined blue saying " W001: Unknown global 'anim' " if that helps.

1 answer

✓ Accepted answer

Hi.

I dunno if you knew this, but ROBLOX has this WACKY method called WaitForChild. It yields a thread until an Instance with a matching name appears.

WHAT THE FRICK IS A WAITFORCHILD?!

WaitForChild is your best friend, and I cannot stress this enough. If a Script says that B is not a valid member of A, and you're 600% certain that something either creates the Instance or the Instance is pre-created in Studio, then that typically means you're trying to index the Instance without it being loaded, which makes Roblox throw an error.

Let's say we have a part. We'll wait two seconds before that part spawns.

wait(2)
local Part = Instance.new('Part', workspace)
Part.Name = 'Super Cool Part'

But, oh no! We really need that part to do XYZ! Not anymore, we can just wait for it!

local Part = workspace:WaitForChild('Super Cool Part')

If ROBLOX decides that you're waiting way too long for the part to generate, it will through an infinite yield possible warning. If YOU decide that you're waiting way too long for the code, maybe you should cut down on the waits during object generation so you can get Kirby levels of super sonic speed.

Now, for your case, you'd be using script:WaitForChild('BlockAnim'). That's it.

Then, your Animation should play and you shouldn't recieve any errors.

The difference between FindFirstChild and WaitForChild.

It's crazy how you can find the difference between A and B almost instantly, just by looking at their names, and maybe doing a little bit of twinkle twinkle little code here and there.

WaitForChild yields the thread in order to wait for the Instance with a matching name of the first argument to appear, as I stated earlier. But, FindFirstChild is an impatient little jerkaloid and searches immediately. If it can't find the part your looking for, then it will return nil. And surprisingly, people fall for this caveat all the time.

ineedsleep.

Oh yeah, by the way. The "processed" variable actually isn't what you expect. The GameProcessedEvent is only true if a GUI with Modal is enabled, like the pause menu or if you're coding a GUI. Generally, if a GUI can release your mouse input, then GameProcessedEvent will return true. You should instead check if the GameProcessedEvent is false, so you can be certain that the player isn't trying to exit the game, or type in the chat.

1 comment

1

Holy wow! This totally helped, I can't thank you enough. I would upvote you if I could but I don't have enough reputation :')

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