Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Running Animation via Key Press, getting error?

Asked by 7 years ago
Edited 7 years ago

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.

0
Please use the code block Decemus 141 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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)
0
I still get the same error, this time at Line 04. SHmods_arebad 0 — 7y
0
I just edit it, try it now ObscureIllusion 352 — 7y
0
Works now, thanks! SHmods_arebad 0 — 7y
0
Hey, since it works now, please accept the answer. It means a lot to me. Thanks! ObscureIllusion 352 — 7y
Ad
Log in to vote
-1
Answered by 7 years ago
Edited 7 years ago

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)
0
I still get the same error, this time at Line 04. SHmods_arebad 0 — 7y

Answer this question