Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to index local 'Humanoid'(A nil value)?

Asked by 5 years ago
Edited 5 years ago

I am trying to make an idle animation stop but when I put in Character:FindFirstChild("Humanoid") it says "20:48:14.460 - Players.360gamer107.Backpack.Desteller.LocalScript:72: attempt to index local 'Humanoid' (a nil value)" The problem is on lines 69 - 77.

local debounce = false                                
local Tool     = script.Parent                        
local Handle   = Tool:WaitForChild("Handle")          
ValueCooldown  = false                                

function SwordCore(hit)                                                         
    local Enemys = hit.Parent:FindFirstChildOfClass("Humanoid")                 
    if script.CanDamage.Value and Enemys then                                   
        local EnemyHit = hit.Parent:FindFirstChild("Enemy")                     
        local PlayerHit = hit.Parent:FindFirstChild("Humanoid")                 
        local Character = Tool.Parent                                           
        local Player = game.Players:GetPlayerFromCharacter(Character)           
        if (not Player) then return end                                         
        local Humanoid = Character:FindFirstChild("Humanoid")                   
        if (not Humanoid) then return end                                       
        if Enemys ~= Humanoid and (not debounce) then                           
            if Enemys.Health <= 0 then return end                                
            debounce = true                                                     
            if PlayerHit then                                                   
                game.ReplicatedStorage.PlayerHit:FireServer(hit,PlayerHit,Tool) 
            end                                                                 
            if EnemyHit then                                                    
                game.ReplicatedStorage.EnemyHit:FireServer(hit,EnemyHit,Tool)   
            end                                                                 
            wait(1)                                                             
            debounce = false                                                    
        end                                                                     
    end                                                                         
end                                                                             
local on = 1
local timer = tick()
function onActivated()                                      
    if (not Tool.Enabled) then return end                   
    Tool.Enabled = false                                    
    if tick()-timer >2 then
        on = 1
    end     
    timer = tick()                          
    local Character = Tool.Parent                               
    local Humanoid = Character:FindFirstChild("Humanoid")   
    local Anim = Humanoid:LoadAnimation(Tool["Slash".. on]) 
    local SlashSound = Handle:WaitForChild("SlashSound")    
    if (Humanoid == nil) then return end                    
    SlashSound:Play()                                       
    Anim:Play()                                             
    Tool.Unequipped:Connect(function()                      
        Anim:Stop()                                         
    end)                                                    
    on = on + 1
    if on >= 4 then
        on = 1
    end
    wait(0.5)                                                   
    Tool.Enabled = true                                     
end

function onEquipped()
    local Character = Tool.Parent                               
    local Humanoid = Character:FindFirstChild("Humanoid")   
    local Idle = Humanoid:LoadAnimation(Tool.Idle)
    if (not Tool) then return end
    if (not Handle) then return end
    Idle:Play()
    if Handle.UnsheathSound.IsPlaying == false then
        Handle.UnsheathSound:Play()
    end
end 

function onUnEquipped()
    local Character = Tool.Parent                               
    local Humanoid = Character:FindFirstChild("Humanoid")   
    local Idle = Humanoid:LoadAnimation(Tool.Idle)
    if (not Tool) then return end
    if (not Handle) then return end
    Idle:Stop()
    Handle.UnsheathSound:Stop()
end


function checkValue()
    if ValueCooldown           == false then
        ValueCooldown          = true
        script.CanDamage.Value = true
        wait(1)
        script.CanDamage.Value = false
        ValueCooldown          = false
    end
end

Tool.Activated:Connect(onActivated) 
Tool.Activated:Connect(checkValue)  
Tool.Equipped:Connect(onEquipped)   
Tool.Unequipped:Connect(onUnEquipped) 

Handle.Touched:Connect(SwordCore)   ``
0
:( User#25115 0 — 5y

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

This is being caused because your Tool is being transferred to the Player's backpack, this will automatically happen everytime you unequip a Tool. Since the Tool's parent becomes 'Backpack', this isn't the Character, nor does it have a Humanoid as a descendant. That's why you're getting Attempt to index local 'Humanoid'. Though the Backpack is a Child of Player and the Character is tied to the Player, so simply find the Player as a Parent of Backpack, then Index their Character's Humanoid from there.

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

Hope this helps! don't forget to accept if so

0
Thank You 360gamer107 6 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

The reason for this error is that once the Tool is unequipped, it is no longer part of the player's character, but rather their Backpack. Because it is now a member of the backpack, and there is no object named "Humanoid" in the backpack, FindFirstChild returns nil, and your attempt to index nil throws an error on line 72.

To solve this, you need to access the player's character through other means, the best being Player.Character.

Lines 69-77 Revised:

function onUnEquipped()
    local Backpack = Tool.Parent;
    local Player = Backpack.Parent;
    local Character = Player.Character;                               
    local Humanoid = Character:FindFirstChild("Humanoid")   
    local Idle = Humanoid:LoadAnimation(Tool.Idle)
    if (not Tool) then return end
    if (not Handle) then return end
    Idle:Stop()
    Handle.UnsheathSound:Stop()
end
0
Thank You 360gamer107 6 — 5y
0
Glad to help! GrandpaScriptin 148 — 5y

Answer this question