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

Why dosent this animation script work?

I have made this script with the aid of the roblox wiki and it seem that when I run the localscript from a players backpack the game Outputs Players.Player1.Backpack.LocalScript:3: attempt to index global 'Humanoid' (a nil value) 15:35:03.697 - Stack Begin 15:35:03.698 - Script 'Players.Player1.Backpack.LocalScript', Line 3 15:35:03.699 - Stack End

Could sombody point out the mistake i must be making

local animation = Instance.new("Animation")
animation.AnimationId = "235600467"
local animTrack = Humanoid:LoadAnimation(animation)
function onKeyDown(key)
    key = key:lower()
    if key == "e" then
    animTrack:Play()
    end
end
script.Parent.Parent:GetMouse().KeyDown:connect(onKeyDown)

1 comment

0

Humanoid is nil, because it has no parent.

2 answers

You forgot to define Humanoid. After line 2, add this:

local Humanoid = nil
local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end
Humanoid = character:WaitForChild("Humanoid")

2 comments

0

Now there are no errors in output and my charecter wont play an animation

0

Can you post your entire script?

This should be a local script if you didn't all ready know.

Wiki tutorial : http://wiki.roblox.com/index.php?title=Animations Demo tutorial : http://www.roblox.com/games/144922434/Animation-Example

local animation = Instance.new("Animation")
local numId = 235600466  --235600467
animation.AnimationId = "http://www.roblox.com/asset/?id="..numId -- Main animation Id

local Humanoid = nil
local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse() -- Gets the local players mouse (Obv)

if not character or not character.Parent then -- Credit to merely here.
    character = player.CharacterAdded:wait()
end

Humanoid = character:WaitForChild("Humanoid")

local animTrack = Humanoid:LoadAnimation(animation)

mouse.KeyDown:connect(function(key)
	if key:lower() == "e" then -- Also its missing the "key" function from the keyboard.
		print(key)
		animTrack:Play()
	end
end)

1 comment

0

Someone correct me if Im incorrent.

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