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

Running Animation via Key Press, getting error?

Okay, so I'm trying to make a crouching script that uses a crouching animation instead of the old c-frame method. The animation (CrouchAnim in the script) is a Child of the Script. Whenever I run it, however, it comes up with the below error. Tbh I'm not even understanding how the Humanoid could be considered not a descendant of the game in this way. Anyone know how to fix this?

The intended result is for the animation to be run by pressing a keyboard button (C in this case). The animation should run, making the character crouch.

*The script is in StarterPack btw.

The Error is:

16:00:57.902 - LoadAnimation requires the Humanoid object (Player1.Humanoid) to be a descendant of the game object 16:00:57.903 - Stack Begin 16:00:57.903 - Script 'Players.Player1.Backpack.Crouch', Line 3 16:00:57.904 - Stack End

Script (error is at Line 3):

local Humanoid = game.Players.LocalPlayer.Character.Humanoid
local Animation = script.CrouchAnim
local Current = Humanoid:LoadAnimation(Animation)
crouching=false
------------------------------------------------------

function onKeyDown(key,mouse)
	key=key:lower()
	if key=="c" then
	if crouching==false then
		crouching=true
Current:Play()
	elseif crouching~=false then
		crouching=false
Current:Stop()

	end
end
end

If you could PM me (Voikov), that'd be great. Thanks.

1 comment

0

Please use the code block

2 answers

✓ Accepted answer

Unfortunately, DaCrazyDev's method is insufficient. The best way to do it is using UserInputService. For example:

wait(2)

local UIS = game:GetService("UserInputService")
local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local Animation = script.CrouchAnim
local Current = Humanoid:LoadAnimation(Animation)
crouching=false
------------------------------------------------------

UIS.InputBegan:connect(function(key, ongui)
	if key.KeyCode == Enum.KeyCode.C and not ongui then
		if crouching==false then
			crouching=true
			Current:Play()
		elseif crouching~=false then
			crouching=false
			Current:Stop()
		end
	end
end)

4 comments

0

I still get the same error, this time at Line 04.

0

I just edit it, try it now

0

Works now, thanks!

0

Hey, since it works now, please accept the answer. It means a lot to me. Thanks!

Maybe it's because the character hasn't loaded yet, and isn't a descendant of the game object yet? Try this on a server test. But yeah, if it is that the character hasn't loaded yet, try this:

game.Players.LocalPlayer.CharacterAdded:connect(function()
	local Humanoid = game.Players.LocalPlayer.Character.Humanoid
	local Animation = script.CrouchAnim
	local Current = Humanoid:LoadAnimation(Animation)
	crouching=false

	function onKeyDown(key,mouse)
    		key=key:lower()
    		if key=="c" then
    			if crouching==false then
        			crouching=true
				Current:Play()
    			elseif crouching~=false then
       			 	crouching=false
				Current:Stop()
end
end
end
end)

1 comment

0

I still get the same error, this time at Line 04.

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