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

How to fix " attempt to index nil with 'FindFirstChild' " while checking for an string in a module?

Asked by 2 years ago

I was making a system which would take your tool and give you a specific amount of gems assigned in a module for each of the tools:

  OmegaCrimsonDumpling = { 
  cost = 14000000000;
  name = "OmegaCrimsonDumpling";
  diamond = 40;

And so basically when I try to check if the tool has the "diamond" string in the module I get an error.

The "check" code:

local module = require(game.ReplicatedStorage.PelmenStats)
workspace.FloppaTraderExchange101.Dialog.DialogChoiceSelected:Connect(function(plr, choice)
    print("1")
    if choice.Name == "Trade" then
        print("2")
        if plr.Character:FindFirstChildWhichIsA("Tool") then
            print("3")
            local tool = plr.Character:FindFirstChildWhichIsA("Tool")
            tostring(tool)
            if module.Stat[tool]:FindFirstChild("diamond") then -- Part with the error.
                print("4")
                print("Success")
            end
        end
    end
end)

Any possible solutions?

0
You only use FindFirstChild on instances. When using tables you have to directly index them like how you did with [tool] greatneil80 2647 — 2y

Answer this question