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