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

attempt to index nil with 'Humanoid' ?

hello guys, I have a problem with my walk / run script. I wanted to add an animation that will play when the player starts to run. But I keep getting an error.

local UserInput = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local sprintspeed = 24
local walkspeed = 16
local sprinting = false 

local function BeginSprint(input,gameProcessed)
	if not gameProcessed then 
		if input.UserInputType == Enum.UserInputType.Keyboard then
			
			local keycode = input.KeyCode
			if  keycode  == Enum.KeyCode.LeftControl then
				sprinting = not sprinting
				if sprinting then 
					Player.Character.Humanoid.WalkSpeed = sprintspeed
					local Anim = Instance.new('Animation')
					Anim.AnimationId = 'rbxassetid://7217974168'
					local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
					PlayAnim:Play()
				else
					Player.Character.Humanoid.WalkSpeed = walkspeed
				end
				 
			end
		end
	end
end


UserInput.InputBegan:connect(BeginSprint)

error:

Players.OleshaDumayet.PlayerScripts.RunningScript:22: attempt to index nil with 'Humanoid'  

I thought maybe I should create a variable for Humanoid like this:

local Humanoid = Player.Character:WaitForChild('Humanoid')

but no its still giving me an error:

 Players.OleshaDumayet.PlayerScripts.RunningScript:4: attempt to index nil with 'WaitForChild'

1 answer

Assuming this is a LocalScript. Change this line:

local Player = game.Players.LocalPlayer
local Character = Player.Character -- << here

To

local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait() -- <<  this will wait for the character to load before continuing!

1 comment

0

yeah you are right, error does not appear anymore, but the problem is that the animation still does not start :(

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