Attempt to index nil with "Running"

I’m trying to make a specific tool animation play whenever the character starts running, but for some reason, it won’t work. Any idea why?

local walkanim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/Asset?ID=9862901789" --Animation ID
walkanim.AnimationId ="http://www.roblox.com/Asset?ID=9946795029"
local human
game.Players.LocalPlayer.CharacterAppearanceLoaded:Connect(function()
human = game.Players.LocalPlayer.Character.Humanoid
end)
local track
--[[
tool.Equipped:Connect(function()
    track = human:LoadAnimation(anim)
    track.Priority = Enum.AnimationPriority.Action
    track.Looped = true
    track:Play()
end)                                                                                        <--- irrelevant to the problem
tool.Unequipped:Connect(function()
    if track then
        track:Stop()
    end
end)
--]]
game.Players.LocalPlayer.Character.Humanoid.Running:Connect(function()
	local track2 = human:LoadAnimation(walkanim)
	if human.Running == true then
	track2.Priority = Enum.AnimationPriority.Action
	track2.Looped = true
	track2:Play()
	elseif human.Running == false then
		track2:Stop()
end
end)
1 Like

Try replacing that line with:
if human:GetState() == Enum.HumanoidStateType.Running then

1 Like

btw the line that makes the function happen doesnt work too, any idea why?
human.StateChanged:Connect(function()
i changed it around a bit
also im using a local script for it

1 Like

Try this, it’s more polished:

local walkanim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/Asset?ID=9862901789" --Animation ID
walkanim.AnimationId ="http://www.roblox.com/Asset?ID=9946795029"
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local human = character.Humanoid
local track
--[[
tool.Equipped:Connect(function()
    track = human:LoadAnimation(anim)
    track.Priority = Enum.AnimationPriority.Action
    track.Looped = true
    track:Play()
end)                                                                                        <--- irrelevant to the problem
tool.Unequipped:Connect(function()
    if track then
        track:Stop()
    end
end)
--]]
human.StateType.Changed:Connect(function(old, new)
	local track2 = human:LoadAnimation(walkanim)
	if new == Enum.HumanoidStateType.Running then
	track2.Priority = Enum.AnimationPriority.Action
	track2.Looped = true
	track2:Play()
	elseif human.Running == false then
		track2:Stop()
end
end)
1 Like

image_2022-06-18_105827771

Consider adding a :WaitForChild(). Usually does the trick.

1 Like

Yeah, using WaitForChild() like what @ifkpop said. So it should look like this:


local walkanim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/Asset?ID=9862901789" --Animation ID
walkanim.AnimationId ="http://www.roblox.com/Asset?ID=9946795029"
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local human = character:WaitForChild("Humanoid")
local track
--[[
tool.Equipped:Connect(function()
    track = human:LoadAnimation(anim)
    track.Priority = Enum.AnimationPriority.Action
    track.Looped = true
    track:Play()
end)                                                                                        <--- irrelevant to the problem
tool.Unequipped:Connect(function()
    if track then
        track:Stop()
    end
end)
--]]
human.StateType.Changed:Connect(function(old, new)
	local track2 = human:LoadAnimation(walkanim)
	if new == Enum.HumanoidStateType.Running then
	track2.Priority = Enum.AnimationPriority.Action
	track2.Looped = true
	track2:Play()
	elseif human.Running == false then
		track2:Stop()
end
end)

image_2022-06-18_190245810

Oops, sorry typo on my end. It should finally work:


local walkanim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/Asset?ID=9862901789" --Animation ID
walkanim.AnimationId ="http://www.roblox.com/Asset?ID=9946795029"
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local human = character:WaitForChild("Humanoid")
local track
--[[
tool.Equipped:Connect(function()
    track = human:LoadAnimation(anim)
    track.Priority = Enum.AnimationPriority.Action
    track.Looped = true
    track:Play()
end)                                                                                        <--- irrelevant to the problem
tool.Unequipped:Connect(function()
    if track then
        track:Stop()
    end
end)
--]]
human.StateChanged:Connect(function(old, new)
	local track2 = human:LoadAnimation(walkanim)
	if new == Enum.HumanoidStateType.Running then
	track2.Priority = Enum.AnimationPriority.Action
	track2.Looped = true
	track2:Play()
	elseif human.Running == false then
		track2:Stop()
end
end)

The quickest solution would be copying the Animator script from the local player (while playtesting) and then inserting it in the same location as it was in the game (for example, if the Animator script is located inside of the starterplayerscripts, you should also put it in the same place in studio). Then simply edit the running animation ID inside the script and the numbervalue inside of the script. It has been a good while since I did something like this, so I cannot assure you this will work. @domboss37’s solution should as well offer the same result (from a logical point of view); I haven’t looked in-depth into their code, but one flaw I have observed is the animationpriority. I’m pretty sure it should be set to Movement, as Action is usually preserved for i.e. tool animations.

1 Like

Good feedback @teeveefy.

I haven’t changed any of the animation stuff, that is all the same as OP’s original code

1 Like

it doesnt work exactly
(the animation plays when you jump only)

@IAmOof_D i think I found the issue,
Change

new ~= Enum.HumanoidStateType.Running

That’s what I did. I modified the code before. Here’s the script right now.

local walkanim = Instance.new("Animation")
local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/Asset?ID=9862901789" --Animation ID
walkanim.AnimationId ="http://www.roblox.com/Asset?ID=9946795029"
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local tool = script.Parent
local human = character:WaitForChild("Humanoid")
local track
local hold = false
tool.Equipped:Connect(function()
    track = human:LoadAnimation(anim)
    track.Priority = Enum.AnimationPriority.Action
    track.Looped = true
    track:Play()
hold = true
end)                                                                                       
tool.Unequipped:Connect(function()
hold = false
    if track then
        track:Stop()
    end
end)

human.StateChanged:Connect(function(old, new)
	local track2 = human:LoadAnimation(walkanim)
if hold == true then
	if new == Enum.HumanoidStateType.Running then
		track2.Priority = Enum.AnimationPriority.Action2
		track2.Looped = true
		track2:Play()
track:Stop()
print("track 1 stop")
		elseif new ~= Enum.HumanoidStateType.Running then
		track2:Stop()
track:Play()
print("track 2 stop")
	end
elseif hold == false then
track2:Stop()
end
end)

might as well make the normal animation the one that plays when running (for the time being)