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

What can I do to fix this error: Argument 1 missing or nil?

Asked by
Jo1nts 134
3 years ago

So im trying to make it where if a player already has a tool in starter gear the npc will say that you already have a weapon, but on line 9 the error says argument 1 missing or nil what am I supposed to do?

cd.MouseClick:Connect(function(plr)
    if  plr.weapon.Value == "Wood Sword"then
    plr.PlayerGui.itemStatus.Text.name.Text = "Sword gal"
    plr.PlayerGui.itemStatus.Text.Text = "You already have this!"
    plr.PlayerGui.itemStatus.Text.Visible = true
    wait(1.7)
    plr.PlayerGui.itemStatus.Text.Visible = false
    else
            if  plr.StarterGear:FindFirstChildOfClass():IsA("Tool") then 
                plr.PlayerGui.itemStatus.Text.name.Text = "Sword gal"
                plr.PlayerGui.itemStatus.Text.Text = "You already have a weapon, if you want to reset it click on your current weapon GUI."
                plr.PlayerGui.itemStatus.Text.Visible = true
                wait(6)
                plr.PlayerGui.itemStatus.Text.Visible = false

2 answers

Log in to vote
0
Answered by 3 years ago

I see multiple problems with this.

Btw I recommend not naming the NPC "Sword gal". You can keep the name if you want.

cd.MouseClick:Connect(function(plr)
    if  plr.weapon.Value == "Wood Sword"then
    plr.PlayerGui.itemStatus.Text.name.Text = "Sword gal"
    plr.PlayerGui.itemStatus.Text.Text = "You already have this!"
    plr.PlayerGui.itemStatus.Text.Visible = true
    wait(1.7)
    plr.PlayerGui.itemStatus.Text.Visible = false
    else
    if plr.StarterGear:FindFirstChildOfClass("Tool") then
        --:IsA("Tool") isnt required and you forgot to put ("Tool") in :FirstChildOfClass()
                plr.PlayerGui.itemStatus.Text.name.Text = "Sword gal"
                plr.PlayerGui.itemStatus.Text.Text = "You already have a weapon, if you want to reset it click on your current weapon GUI."
                plr.PlayerGui.itemStatus.Text.Visible = true
                wait(6)
                plr.PlayerGui.itemStatus.Text.Visible = false
    end
0
There's nothing wrong with having an NPC of that name? You can really reference it using square brackets instead of indexing it using a period. DeceptiveCaster 3761 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

[For the complete and correct answer, see CandyWreckEpicness' answer below this one.] [This answer is wrong, and I stand corrected. See comments under this post]

You are just finding if the first child of StarterGear is a tool. Since some players might not have any Tool, Line 9 will give you '1 missing or nil', since you are asking if "nothing" is a tool.

What happens is:

  1. Get the child of Starter Gear
  2. Is it a tool?

To fix this, you should check first if a child exist.

-Is there a child?/Is it not NIL?

*There is a child.

  1. Get the child.
  2. Is it a tool?

*There is no child.

  1. Do nothing
if plr.StarterGear:FindFirstChildOfClass() then
     if plr.StarterGear:FindFirstChildOfClass():IsA("Tool") then
          plr.PlayerGui.itemStatus.Text.name.Text = "Sword gal"
          plr.PlayerGui.itemStatus.Text.Text = "You already have a weapon, if you want to reset it 
          click on your current weapon GUI."
          plr.PlayerGui.itemStatus.Text.Visible = true
          wait(6)
          plr.PlayerGui.itemStatus.Text.Visible = false
     end
end

If Line 1 does not work, try

if plr.StarterGear:FindFirstChildOfClass() ~= nil then

But in my experience, "if yes" works the same way as "if not nil"

0
Your script will not work. :FindFirstChildOfClass requires a string in the (). The string needs to contain the type of object it wants to target therefore, tool. So you put "Tool" inside. Because of this, :IsA isn't required at all and doesn't need any extra stuff. The script with this is below this answer. CandyWreckEpicness 115 — 3y
0
And if you use any type :FindFirstChild() if it doesn't exist, then it will do nothing. I experienced that recently while making my simulator. CandyWreckEpicness 115 — 3y
0
That bit of information was very helpful. I have edited my entry to point to your answer, but I will not delete my entry to show visitors what was wrong with my script and hopefully learn with me. Thank you! ConsteIeo 63 — 3y

Answer this question