Output getting spammed with errors due to a script

Hey, so i have a sword script and at the line where it says "if humanoid.Parent ~= tool.Parent and debounce == true and humanoid.Health > 0 and tool.Parent ~= player.backpack then it gives an error saying “attempt to index nil with Parent” and it spams the output with these errors, which is very annoying. and the script works fine as well. can someone help me fix this issue?

here is the code below (yes its long)

tool.Hitbox.Touched:connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    local hum = tool.Parent.Humanoid
    local player = game.Players:GetPlayerFromCharacter(tool.Parent)
    local eplayer = game.Players:GetPlayerFromCharacter(hit.Parent)
    if humanoid.Parent ~= tool.Parent and debounce == true and humanoid.Health > 0 and tool.Parent ~= player.Backpack then
        humanoid.Health -= 8.5
        debounce = false
        local tagval = hit.Parent:FindFirstChildWhichIsA("IntValue")
        tagval.Name = player.Name
        lbremote:FireClient(player)
        
        local player = game.Players:GetPlayerFromCharacter(tool.Parent)
        if MS:UserOwnsGamePassAsync(player.UserId,x2) then
            player.leaderstats.Swings.Value = player.leaderstats.Swings.Value + 2
            else
            player.leaderstats.Swings.Value = player.leaderstats.Swings.Value + 1
            end
            
        local clonekillscript = game.ServerStorage.DebuffScripts.KillLB:Clone()

        if hit.Parent:FindFirstChild("KillLB") == nil then
            if humanoid.Health > 0 then
                clonekillscript.Parent = humanoid.Parent
            end


        else
            if humanoid.Health > 0 then
                hit.Parent.KillLB:Destroy()
                clonekillscript.Parent = humanoid.Parent
            end


        end
    


    
    end
    
    
end)

Add checks to verify that humanoid is not nil

tool.Hitbox.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    local hum = tool.Parent:FindFirstChild("Humanoid") -- Added FindFirstChild to be safe
    local player = game.Players:GetPlayerFromCharacter(tool.Parent)
    if humanoid and hum and player then
        if humanoid.Parent ~= tool.Parent and debounce == true and humanoid.Health > 0 and tool.Parent ~= player.Backpack then
            humanoid.Health -= 8.5
            debounce = false
            local tagval = hit.Parent:FindFirstChildWhichIsA("IntValue") or Instance.new("IntValue", hit.Parent)
            tagval.Name = player.Name
            lbremote:FireClient(player)
            if MS:UserOwnsGamePassAsync(player.UserId, x2) then
                player.leaderstats.Swings.Value = player.leaderstats.Swings.Value + 2
            else
                player.leaderstats.Swings.Value = player.leaderstats.Swings.Value + 1
            end
            local clonekillscript = game.ServerStorage.DebuffScripts.KillLB:Clone()
            if not hit.Parent:FindFirstChild("KillLB") then
                if humanoid.Health > 0 then
                    clonekillscript.Parent = humanoid.Parent
                end
            else
                if humanoid.Health > 0 then
                    hit.Parent.KillLB:Destroy()
                    clonekillscript.Parent = humanoid.Parent
                end
            end
        end
    end
end)

Updated script with safety checks try it out

1 Like

thank you so much, i knew i had to do checks but i didnt know how.