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)
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
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.
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 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?
local function EquippedAnimations()
Humanoid.Running:Connect(function(speed)
if speed > 0 then
AnimationTrackWalk:Play()
AnimationTrackIdle:Stop()
else
AnimationTrackWalk:Stop()
AnimationTrackIdle:Play()
end
end)
end