Search code examples
luaroblox

Attemp to index nil with 'Value'


im going to make a switcher. It means when you equip that tool, the switcher(serverscript) will turn on the body system inside a folder and in the ServerScriptService i disabled them all, . Here is the Switcher code.

local Tool = script.Parent
local SSS = game.ServerScriptService:WaitForChild("CombatFolder")

function onEquippedLocal()
    SSS:WaitForChild("CanAttack").Disabled = false
    SSS:FindFirstChild("M1").Disabled = false

    SSS:FindFirstChild("M2").Disabled = false

end

function onUnequippedLocal()
    SSS:WaitForChild("CanAttack").Disabled = true
    SSS:FindFirstChild("M1").Disabled = true

    SSS:FindFirstChild("M2").Disabled = true

end

Tool.Equipped:connect(onEquippedLocal)
Tool.Unequipped:connect(onUnequippedLocal)

and this is the output when i equip the tool

  10:50:22.732  Players.TeemoHoang_2008.Backpack.SwordCombat.M2:15: attempt to index nil with 'Value'  -  Client - M2:15
  10:50:22.732  Stack Begin  -  Studio
  10:50:22.732  Script 'Players.TeemoHoang_2008.Backpack.SwordCombat.M2', Line 15  -  Studio - M2:15
  10:50:22.732  Stack End  -  Studio
  10:50:24.099  Players.TeemoHoang_2008.Backpack.SwordCombat.newCombat:54: attempt to index nil with 'Value'  -  Client - newCombat:54
  10:50:24.099  Stack Begin  -  Studio
  10:50:24.099  Script 'Players.TeemoHoang_2008.Backpack.SwordCombat.newCombat', Line 54  -  Studio - newCombat:54
  10:50:24.099  Stack End  -  Studio

here is the function when the error start in the M2(local script) in the tool

script.Parent.Equipped:Connect(function()
    uis.InputBegan:Connect(function(input)
        if input.UserInputType == Enum.UserInputType.MouseButton2  then
            if debounce == false and char:FindFirstChild("CanAttack").Value == true and char:FindFirstChild("Stun") == nil and char:FindFirstChild("eStun") == nil and char:FindFirstChild("BlockBreak") == nil and char:FindFirstChild("IsBlocking") == nil then
                if  char:FindFirstChild("SwordCombat") then
                    local tool =  char:FindFirstChild("SwordCombat")
                    if tool:IsA("Tool")then
                        debounce = true

                        remote:FireServer() 
                    end
                end     
            end
        end
    end)
end)

and this is the error line of the newCombat(localscript) inside the tool

script.Parent.Activated:Connect(function()
    if char:FindFirstChild("CanAttack").Value == true and char:FindFirstChild("Stun") == nil and char:FindFirstChild("eStun") == nil and char:FindFirstChild("IsBlocking") == nil and char:FindFirstChild("BlockBreak") == nil then

i have tried to disabled the newCombat and M2 and enable it when equip the tool same time as the body system but when i do that. M2 only shows error but everything still doesn't work. Pls help!


Solution

  • The error is pretty obvious. You're trying to index a nil value with Value so char:FindFirstChild("CanAttack") returns nil.

    Instance:FindFirstChilde(name) returns nil if there is no child with the given name.

    Find out why there is no such child or check wether there is actually a child befor you try to index it.