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

I can't access my player's Humanoid?

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

✓ Accepted answer
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.

2 comments

0

Thank you!

0

wht this is not how you wait for the players character. Use events not loops. -1

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