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

Why dosent this animation script work?

Asked by 9 years ago

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)

0
Humanoid is nil, because it has no parent. woodengop 1134 — 9y

2 answers

Log in to vote
0
Answered by
Merely 2122 Moderation Voter Community Moderator
9 years ago

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")
0
Now there are no errors in output and my charecter wont play an animation ZincPlays 0 — 9y
0
Can you post your entire script? Merely 2122 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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)
0
Someone correct me if Im incorrent. MessorAdmin 598 — 9y

Answer this question