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?
01 | cd.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 |
I see multiple problems with this.
Btw I recommend not naming the NPC "Sword gal". You can keep the name if you want.
01 | cd.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 |
[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:
To fix this, you should check first if a child exist.
-Is there a child?/Is it not NIL?
*There is a child.
*There is no child.
01 | if 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 |
10 | end |
If Line 1 does not work, try
1 | if plr.StarterGear:FindFirstChildOfClass() ~ = nil then |
But in my experience, "if yes" works the same way as "if not nil"