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

try to index nil with FindFirstChild Help What do I do? It is a SaveTools

Asked by 3 years ago
    local Backpack = Player:WaitForChild("Backpack")
local StarterGear = Player:WaitForChild("StarterGear")

if ToolData ~= nil then
    for i, v in pairs(ToolData) do
        if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
            ToolFolder[v]:Clone().Parent = Backpack
            ToolFolder[v]:Clone().Parent = StarterGear
        end
    end
end

Player.CharacterRemoving:Connect(function(Character)
    Character:WaitForChild("Humanoid"):UnequipTools()
end)

Some parts of the script I deleted it because it was not shown so I deleted some parts I am showing in the part where the error is

On line 6

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

FindFirstChild will return true or false when used in an if statement saying if it exists or not checking if it is nil will return an error and you should check if it is not StarterGear:FindFirstChild(v) or if StarterGear:FindFirstChild(v) == false I fixed your code below.

    local Backpack = Player:WaitForChild("Backpack")
local StarterGear = Player:WaitForChild("StarterGear")

if ToolData ~= nil then
    for i, v in pairs(ToolData) do
        if ToolFolder:FindFirstChild(v) and not Backpack:FindFirstChild(v) and not StarterGear:FindFirstChild(v) then -- Checking if FindFirstChild(v) is nil can not be checked as it returns a bool value.
            ToolFolder[v]:Clone().Parent = Backpack
            ToolFolder[v]:Clone().Parent = StarterGear
        end
    end
end

Player.CharacterRemoving:Connect(function(Character)
    Character:WaitForChild("Humanoid"):UnequipTools()
end)
Ad

Answer this question