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

I can't access my player's Humanoid?

Asked by 5 years ago

I'm trying to animate a tool in my player's starterpack, but for some reason I keep getting an error message that says "attempt to index field 'Character' (a nil value)" referring to line 3. I've tried several variations where I make the script wait for everything to load, but none of those worked either.

This is the code:

tool = script.Parent
local animation = tool.SwingAnimation
humanoid = game.Players.LocalPlayer.Character.Humanoid
local track = humanoid:LoadAnimation(animation) --loads the animation into humanoid

tool.Activated:Connect(function()
    track:Play()
    wait(1)
end)

tool.Deactivated:Connect(function()
    track:Stop()
end)

What am I doing wrong? Thanks!

1 answer

Log in to vote
-1
Answered by 5 years ago
tool = script.Parent
local animation = tool.SwingAnimation
local player = game.Players.LocalPlayer

repeat wait() until player.Character ~= nil and workspace:FindFirstChild(player.Name) ~= nil
local humanoid = player.Character:WaitForChild("Humanoid")
local track = humanoid:LoadAnimation(animation)

tool.Activated:Connect(function()
    track:Play()
    wait(1)
end)

tool.Deactivated:Connect(function()
    track:Stop()
end)

You need to wait for the character to load into the workspace before trying to define the humanoid.

0
Thank you! Flannelling 4 — 5y
0
wht this is not how you wait for the players character. Use events not loops. -1 User#5423 17 — 5y
Ad

Answer this question