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
4 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?

01cd.MouseClick:Connect(function(plr)
02    if  plr.weapon.Value == "Wood Sword"then
03    plr.PlayerGui.itemStatus.Text.name.Text = "Sword gal"
04    plr.PlayerGui.itemStatus.Text.Text = "You already have this!"
05    plr.PlayerGui.itemStatus.Text.Visible = true
06    wait(1.7)
07    plr.PlayerGui.itemStatus.Text.Visible = false
08    else
09            if  plr.StarterGear:FindFirstChildOfClass():IsA("Tool") then
10                plr.PlayerGui.itemStatus.Text.name.Text = "Sword gal"
11                plr.PlayerGui.itemStatus.Text.Text = "You already have a weapon, if you want to reset it click on your current weapon GUI."
12                plr.PlayerGui.itemStatus.Text.Visible = true
13                wait(6)
14                plr.PlayerGui.itemStatus.Text.Visible = false

2 answers

Log in to vote
0
Answered by 4 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.

01cd.MouseClick:Connect(function(plr)
02    if  plr.weapon.Value == "Wood Sword"then
03    plr.PlayerGui.itemStatus.Text.name.Text = "Sword gal"
04    plr.PlayerGui.itemStatus.Text.Text = "You already have this!"
05    plr.PlayerGui.itemStatus.Text.Visible = true
06    wait(1.7)
07    plr.PlayerGui.itemStatus.Text.Visible = false
08    else
09    if plr.StarterGear:FindFirstChildOfClass("Tool") then
10        --:IsA("Tool") isnt required and you forgot to put ("Tool") in :FirstChildOfClass()
11                plr.PlayerGui.itemStatus.Text.name.Text = "Sword gal"
12                plr.PlayerGui.itemStatus.Text.Text = "You already have a weapon, if you want to reset it click on your current weapon GUI."
13                plr.PlayerGui.itemStatus.Text.Visible = true
14                wait(6)
15                plr.PlayerGui.itemStatus.Text.Visible = false
16    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 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 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
01if plr.StarterGear:FindFirstChildOfClass() then
02     if plr.StarterGear:FindFirstChildOfClass():IsA("Tool") then
03          plr.PlayerGui.itemStatus.Text.name.Text = "Sword gal"
04          plr.PlayerGui.itemStatus.Text.Text = "You already have a weapon, if you want to reset it
05          click on your current weapon GUI."
06          plr.PlayerGui.itemStatus.Text.Visible = true
07          wait(6)
08          plr.PlayerGui.itemStatus.Text.Visible = false
09     end
10end

If Line 1 does not work, try

1if 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 — 4y
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 — 4y
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 — 4y

Answer this question