Getting index nil with 'running'

I’ve been trying to script a sword walk/idle movement system for a combat system i work on.

I’ve had to redesign the code several different times and the only thing holding it back is this error
Error:

Workspace.Scimitar.Script:59: attempt to index nil with 'Running' - Server - Script:59

Here’s the code


local Humanoid = script.Parent:FindFirstChild("Humanoid")


Humanoid.Running:Connect(function(speed)
	if tool.Equipped then
		local Sword_Walk_Animation = Instance.new("Animation")
		Sword_Walk_Animation.AnimationId = "rbxassetid://9404399317"
		local AnimationTrackWalk = Humanoid:LoadAnimation(Sword_Walk_Animation)
		local Sword_Idle_Animation = Instance.new("Animation")
		Sword_Idle_Animation.AnimationId = "rbxassetid://9404394687"
		local AnimationTrackIdle = Humanoid:LoadAnimation(Sword_Idle_Animation)
		if speed > 0 then 
			AnimationTrackWalk:Play()
			AnimationTrackIdle:Stop()
		elseif speed == 0 then -- 
			AnimationTrackWalk:Stop()
			AnimationTrackIdle:Play()
		end
		end
	end)

Heya!

I would suggest using WaitForChild("Humanoid") instead of FindFirstChild("Humanoid"). That could possibly the fix the issue already.

If not, then it seems like humanoid is not in the location where you are trying to get it from, try adding a check for that.

print(script.Parent.Name) -- Check if it prints the user's name, if not then the humanoid can't be there
local Humanoid = script.Parent:WaitForChild("Humanoid") -- Use :WaitForChild() to see if that fixes it.

if Humanoid then
   print("Found") 
end

Share your results here after running the code above :slight_smile:

I got the error

Infinite yield possible on 'Workspace.Scimitar:WaitForChild("Humanoid")'

I never really understand what the correct way to get Humanoid is, so I may not be very helpful here.

The error

Infinite yield possible on 'Workspace.Scimitar:WaitForChild("Humanoid")

basically means it is constantly waiting to find a humanoid in script.Parent. So you are trying to get a humanoid from an object that has no humanoid.

Could you tell me where the script is located at?

The script is located with the Scimitar

You should do script.Parent.Parent:WaitForChild("Humanoid") instead of script.Parent:WaitForChild("Humanoid").

Because the script is getting it’s parent (tool), and not getting the parent of the tool (which would be the player’s character).

Damn that’s a steal right there

1 Like

I’ve actually tried that, making it

Infinite yield possible on 'Workspace:WaitForChild("Humanoid")'

Alright, it’s normal. You need to like, get the tool inside of the player’s character.

Yes, because this is running as soon as soon as the server loads. It does not wait until the tool is equipped by some player, and at the time when that code is executed, script.Parent.Parent evaluates to Workspace. One way you could do this is by listening for the Tool.Equipped signal and then define and use the humanoid as needed.

Example:

local tool = script.Parent

local runningSignal

tool.Equipped:Connect(function()
    local character = script.Parent
    local humanoid = character:FindFirstChildOfClass("Humanoid")

    if humanoid and not runningSignal then
        runningSignal = humanoid.Running:Connect(function()
            -- your code here
        end)
    end
end)

Can you tell us how the tool is being given. Because from what i understand, the tool is parented directly under workspace, which means that it should either be picked up by a player, or a script would give the tool to a player.

Using your logic, how would I detect idleSignal, sorry I haven’t done much research on this.

Well, Humanoid.Running gives you the speed, an example would look like this:

humanoid.Running:Connect(function(speed)
     if speed > 0 then
          -- running
     else
          -- not running/walking
     end
end)
1 Like

Your code works but it has a lot of delay between idle/running changes.
Is this because it’s running instead of walking, therfore it’s not having anything for the slow?

I’m not sure what you mean by delayed, it is just a signal so it should work fine. It might be the code you are putting inside.

I keep having to rewrite the code, and each time a new error, wonderful!

local Character = tool.Parent
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Sword_Walk_Animation = Instance.new("Animation")
Sword_Walk_Animation.AnimationId = "rbxassetid://9404399317"
local AnimationTrackWalk = Humanoid:LoadAnimation(Sword_Walk_Animation)
local Sword_Idle_Animation = Instance.new("Animation")
Sword_Idle_Animation.AnimationId = "rbxassetid://9404394687"
local AnimationTrackIdle = Humanoid:LoadAnimation(Sword_Idle_Animation)

local function EquippedAnimations()
	Humanoid.Running:Connect(function(speed)
		if speed > 0 then
			AnimationTrackWalk:Play()
			AnimationTrackIdle:Stop()
		else
			AnimationTrackWalk:Stop()
			AnimationTrackIdle:Play()
		end
	end)

Error: Workspace.Scimitar.Script:80: Expected 'end' (to close 'function' at line 66), got <eof>; did you forget to close 'function' at line 67?

I honestly don’t know.

You forgot an end, the error states that:

local function EquippedAnimations()
	Humanoid.Running:Connect(function(speed)
		if speed > 0 then
			AnimationTrackWalk:Play()
			AnimationTrackIdle:Stop()
		else
			AnimationTrackWalk:Stop()
			AnimationTrackIdle:Play()
		end
	end)
end
1 Like

Oh great tysm, sorry I must’ve missed that, I’ve re-written it so many times.

Also, I’d like to add, avoid using Humanoid:LoadAnimation, you should be using Humanoid.Animator:LoadAnimation as the former is deprecated.

How should I define “animator”
Workspace.Scimitar.Script:62: attempt to index nil with ‘Animator’