#Attempt to index nil with 'LoadAnimation'

25 messages · Page 1 of 1 (latest)

glacial patio
#
local Tool = script.Parent
local call= Tool.callAnim
local idle = Tool.idleAnim
local isFinished = true

local humanoid = Tool.Parent:FindFirstChild("Humanoid")

Tool.Activated:Connect(function()
    if isFinished then
        isFinished = false
        local callAnim= humanoid:LoadAnimation(call)
        if callAnim then
            local length = callAnim.Length
            callAnim:Start()
            wait(length)
            callAnim:Stop()
        else
            warn("Call animation not found.")
        end
        wait(0.3)
        isFinished = true
    end
end)

Tool.Equipped:Connect(function()
    if humanoid then
        local idleAnim = humanoid:LoadAnimation(idle)
        if idleAnim then
            idleAnim:Play()
        else
            warn("Idle animation not found.")
        end
    end
end)

Tool.Unequipped:Connect(function()
    if humanoid then
        local animator = humanoid:FindFirstChild("Animator")
        if animator then
            animator:StopAllAnimations()
        end
    end
end)

Hello, I'm just trying out Roblox Studio and I got error message:

Players.rovnone.Backpack.Mobile.LocalScript:11: attempt to index nil with 'LoadAnimation'

Does anyone know how can I fix this issue?
Thanks, Radaros.

shrewd shadow
#

replace FindFirstChild with WaitForChild on line 6

glacial patio
shrewd shadow
#

because there is no humanoid in Backpack

#

put the entire 6th line inside Tool.Activated

glacial patio
shrewd shadow
#

put local humanoid = ... into the first line of unequip

glacial patio
glacial patio
stray deltaBOT
#

studio** You are now Level 1! **studio

shrewd shadow
#
-- Define the tool and animations
local Tool = script.Parent
local Idle = Tool.idleAnim
local Call = Tool.callAnim

-- Define variables to store the animation tracks
local IdleAnim
local CallAnim

-- Function to run when the tool is activated
local function ToolActivated()
    -- Check if the tool is enabled, if so, exit the function
    if Tool.Enabled then
        return
    end
    
    -- Check if the animations are loaded
    if not (IdleAnim and CallAnim) then
        return
    end

    -- Set the tool as enabled
    Tool.Enabled = true
    
    -- Start the call animation
    CallAnim:Play()

    -- Wait for the call animation to stop
    CallAnim.Stopped:Wait()

    -- Wait a bit before disabling the tool
    task.wait(0.3)
    Tool.Enabled = false
end

-- Function to run when the tool is equipped
local function ToolEquipped()
    -- Get the character and humanoid
    local Character = Tool.Parent
    local Humanoid = Character:FindFirstChild("Humanoid")

    -- If there's no humanoid, exit the function
    if not Humanoid then
        return
    end

    -- Load animations
    IdleAnim = Humanoid:LoadAnimation(Idle)
    CallAnim = Humanoid:LoadAnimation(Call)

    -- Set the idle to animation loop
    IdleAnim.Looped = true

    -- Start the idle animation
    IdleAnim:Play()
end

-- Function to run when the tool is unequipped
local function ToolUnequipped()
    -- Check if the animations are loaded
    if not (IdleAnim and CallAnim) then
        return
    end
    
    -- Stop the idle and call animations
    IdleAnim:Stop()
    CallAnim:Stop()
end

-- Connect the functions to the tool's events
Tool.Activated:Connect(ToolActivated)
Tool.Equipped:Connect(ToolEquipped)
Tool.Unequipped:Connect(ToolUnequipped)

try this code

#

I hope it works lol

#

I didnt test it in any way

#

this should give you more understanding (hopefully) on how tools work idk

#

if it does not work, try to add prints before returns and figure out whats wrong yourself

#

@glacial patio

glacial patio
shrewd shadow
#

wait

#

turns out that Humanoid:LoadAnimation is deprecated

#

its better to use Animator

#

i didnt know that

#
-- Define the tool and animations
local Tool = script.Parent
local Idle = Tool.idleAnim
local Call = Tool.callAnim

-- Define variables to store the animation tracks
local IdleAnim
local CallAnim

-- Function to run when the tool is activated
local function ToolActivated()
    -- Check if the tool is enabled, if so, exit the function
    if Tool.Enabled then
        return
    end

    -- Check if the animations are loaded
    if not (IdleAnim and CallAnim) then
        return
    end

    -- Set the tool as enabled
    Tool.Enabled = true
    
    -- Start the call animation
    CallAnim:Play()

    -- Wait for the call animation to stop
    CallAnim.Stopped:Wait()

    -- Wait a bit before disabling the tool
    task.wait(0.3)
    Tool.Enabled = false
end

-- Function run to when the tool is equipped
local function ToolEquipped()
    -- Get the character and humanoid
    local Character = Tool.Parent
    local Humanoid = Character:FindFirstChild("Humanoid")

    -- If there's no humanoid, exit the function
    if not Humanoid then
        return
    end

    -- Get the animator
    local Animator = Humanoid:FindFirstChildOfClass("Animator")

    -- If there's no animator, exit the function
    if not Animator then
        return
    end

    -- Load the idle and call animations
    IdleAnim = Animator:LoadAnimation(Idle)
    CallAnim = Animator:LoadAnimation(Call)

    -- Set the idle to animation loop
    IdleAnim.Looped = true

    -- Start the idle animation
    IdleAnim:Play()
end

-- Function to run when the tool is unequipped
local function ToolUnequipped()
    -- Check if the animations are loaded
    if not (IdleAnim and CallAnim) then
        return
    end

    -- Stop the idle and call animations
    IdleAnim:Stop()
    CallAnim:Stop()
end

-- Connect the functions to the tool's events
Tool.Activated:Connect(ToolActivated)
Tool.Equipped:Connect(ToolEquipped)
Tool.Unequipped:Connect(ToolUnequipped)
#

check if this code works

glacial patio
#

works ty

#

@shrewd shadow