Literally after only 10 Minutes of debugging and everything is fixed but now I meet this issue again for the millionth time
Code
-- Variables
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character.Humanoid
local Animate = Character.Animate
-- sample code not actually written
print(Humanoid.WalkSpeed)
Output
Players.FerbZides.Backpack.Wooden Sword.InitClient:4: attempt to index nil with 'Humanoid' - Client - InitClient:4
13:26:25.825 Stack Begin - Studio
13:26:25.825 Script 'Players.FerbZides.Backpack.Wooden Sword.InitClient', Line 4 - Studio - InitClient:4
13:26:25.825 Stack End - Studio
You’re most likely indexing the Humanoid instance before the Character is loaded. You can try waiting for the character to be added using Player:CharacterAdded:Wait() as an alternative in case Player.Character is nil when indexed.
-- Variables
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- wait until the character is added if unavailable
local Humanoid = Character.Humanoid
local Animate = Character.Animate
-- sample code not actually written
print(Humanoid.WalkSpeed)
-- Variables
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Animate = Character:FindFirstChild("Animate")
yes and the old code I wrote some time ago still has the humanoid but this code doesn’t here and the output error is:
Players.FerbZides.Backpack.Wooden Sword.InitClient:11: attempt to index nil with 'GetPlayingAnimationTracks' - Client - InitClient:11
13:47:28.070 Stack Begin - Studio
13:47:28.070 Script 'Players.FerbZides.Backpack.Wooden Sword.InitClient', Line 11 - function ChangeAnimations - Studio - InitClient:11
13:47:28.071 Script 'Players.FerbZides.Backpack.Wooden Sword.InitClient', Line 36 - Studio - InitClient:36
13:47:28.071 Stack End - Studio
even though it is not the humanoid not existing its an attempt to index nil object with function
Can you post the entire script or at least up until line 11? You only provided a section with your variables and none of us will really be able to figure out what’s going on unless we can see more of the code throwing an error. It looks like the original problem you posted about has been resolved though.
-- Variables
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Animate = Character:FindFirstChild("Animate")
local Events = script.Parent.Events
local PlayAnimationsEvent = Events.PlayAnimation
local function ChangeAnimations(Enabled)
if Enabled == true then
for _, PlayingTracks in pairs(Humanoid:GetPlayingAnimationTracks()) do
PlayingTracks:Stop()
end
Animate.idle.Animation1.AnimationId = "rbxassetid://5991049684"
Animate.idle.Animation2.AnimationId = "rbxassetid://5991049684"
Animate.run.RunAnimation.AnimationId = "rbxassetid://6012455420"
Animate.walk.WalkAnimation.AnimationId = "rbxassetid://6012455420"
Animate.jump.JumpAnimation.AnimationId = "rbxassetid://6012503860"
elseif Enabled == false then
for _, PlayingTracks in pairs(Humanoid:GetPlayingAnimationTracks()) do
PlayingTracks:Stop()
end
Animate.idle.Animation1.AnimationId = "rbxassetid://507766388"
Animate.idle.Animation2.AnimationId = "rbxassetid://507766666"
Animate.run.RunAnimation.AnimationId = "rbxassetid://913376220"
Animate.walk.WalkAnimation.AnimationId = "rbxassetid://913402848"
Animate.jump.JumpAnimation.AnimationId = "rbxassetid://507765000"
end
end
PlayAnimationsEvent.OnClientEvent:Connect(function(Output)
if Output == "Play" then
ChangeAnimations(true)
elseif Output == "Stop" then
ChangeAnimations(false)
elseif Output == "Swing" then
local NewAnimator = Instance.new("Animator")
NewAnimator.Name = "FakeTrack"
NewAnimator.Parent = Character
local FakeAnimationObject = Instance.new("Animation")
FakeAnimationObject.Name = "FakeAnimation"
FakeAnimationObject.Parent = NewAnimator
FakeAnimationObject.AnimationId = "rbxassetid://5991043699"
local AnimationTrack = NewAnimator:LoadAnimation(FakeAnimationObject)
AnimationTrack:Play()
AnimationTrack.Stopped:wait()
NewAnimator:Destroy()
end
end)
oh and also here is the old code btw
-- Services
local UIS = game:GetService("UserInputService")
-- Variables
local Tool = script.Parent.Parent.Parent.Parent
local Events = Tool:WaitForChild("Events")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character.Humanoid
-- Shiftlock
local PlayerScripts = Player.PlayerScripts
local Values = PlayerScripts.Values
local IsHoldingTool = Values.IsHoldingTool
-- UI
local PlayerGui = Player.PlayerGui
local MouseIconFolder = PlayerGui.MouseIcon
local WeaponMouseIcon = MouseIconFolder.WeaponMouseIcon
-- Events
local PlayAnimation = Events.PlayAnimation
local ChangeMouse = Events.ChangeMouse
local Shiftlock = Events.Shiftlock
local Motor6D = Events.Motor6D
-- Animation Tracks
local AnimationsFolder = Tool.Animations
local jumpAnimation = AnimationsFolder.Jump
local walkAnimation = AnimationsFolder.Walk
local swingAnimation = AnimationsFolder.Swing
local holdAnimation = AnimationsFolder.Hold
-- Functions
local function CharacterAnim(bool)
local Animate = Character.Animate
if bool == true then -- it says enable the wooden sword walk
for DescendantName, DescendantObject in pairs(Animate:GetDescendants()) do
local counter = 0
if counter < 19 then -- amount of animations
if DescendantObject:IsA("AnimationTrack") then
local Animation = Humanoid:LoadAnimation(DescendantObject)
Animation:Stop()
end
else
break;
end
end
Animate.idle.Animation1.AnimationId = holdAnimation.AnimationId
Animate.idle.Animation2.AnimationId = holdAnimation.AnimationId
Animate.walk.WalkAnim.AnimationId = walkAnimation.AnimationId
Animate.run.RunAnim.AnimationId = walkAnimation.AnimationId
Animate.jump.JumpAnim.AnimationId = jumpAnimation.AnimationId
elseif bool == false then -- disable it
for DescendantName, DescendantObject in pairs(Animate:GetDescendants()) do
local counter = 0
if counter < 19 then -- amount of animations
if DescendantObject:IsA("AnimationTrack") then
local Animation = Humanoid:LoadAnimation(DescendantObject)
Animation:Stop()
end
else
break;
end
end
Animate.idle.Animation1.AnimationId = "rbxassetid://507766388"
Animate.idle.Animation2.AnimationId = "rbxassetid://507766666"
Animate.walk.WalkAnim.AnimationId = "rbxassetid://913402848"
Animate.run.RunAnim.AnimationId = "rbxassetid://913376220"
Animate.jump.JumpAnim.AnimationId = "rbxassetid://507765000"
end
end
local function Shiftlock(Response)
if Response == true then
IsHoldingTool.Value = true
elseif Response == false then
IsHoldingTool.Value = false
end
end
local function ChangeMouseIcon(MouseIconEnabled)
if MouseIconEnabled == true then
UIS.MouseIconEnabled = false
WeaponMouseIcon.Enabled = true
elseif MouseIconEnabled == false then
UIS.MouseIconEnabled = true
WeaponMouseIcon.Enabled = false
end
end
Tool.Equipped:Connect(function()
Motor6D:FireServer(true, Tool.Sword)
CharacterAnim(true)
Shiftlock(true)
ChangeMouseIcon(true)
end)
Tool.Unequipped:Connect(function()
Motor6D:FireServer(false)
CharacterAnim(false)
Shiftlock(false)
ChangeMouseIcon(false)
end)
PlayAnimation.OnClientEvent:Connect(function(AnimationName)
if AnimationName == "Swing" then
local swingAnimationTrack = Humanoid:LoadAnimation(swingAnimation)
-- Play it bruh lol
swingAnimationTrack:Play()
end
end)
I’d recommend re-reading the documentation for GetPlayingAnimationTracks() to make sure you aren’t using it improperly. It’s hard to tell if you’re attempting to do this or not, but AnimationTracks will not return animations that are loaded but not playing.
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- wait until the character is added if unavailable
local Humanoid = Character:WaitForChild("Humanoid") -- wait for the humanoid too
local Animate = Character.Animate