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!
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.