Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Removing tools from players character or backpack?

I want to remove these weapons when I click the parent of this script. It attempted to index local 'player' (a number value)?

local player = game.Players.LocalPlayer
local weapons = {
    "AK-104",  
    "AK-12",
    "AK-47",
	"AK-74",
	"AKM",
	"AKS-74U",
	"AN-94",
	"Auto-5",
	"CBJ-MS",
	"CZ-75",
	"Enfield",
	"FAL",
	"Federov",
	"G18",
	"G3",
	"G36K",
	"G37",
	"HK417",
	"M1 Garand",
	"M1014",
	"M14",
	"M1911",
	"M249",
	"M3",
	"M4A1",
	"M870",
	"M9",
	"M93R",
	"MP5",
	"Makarov",
	"Maverick",
	"Mk 17",
	"Mk 23",
	"Mk 48",
	"Model 459",
	"Mosin-Nagant",
	"OTs-14",
	"PP-19",
	"Patriot",
	"RPK",
	"Revolver",
	"SCAR-L",
	"SKS",
	"Shotgun",
	"TEC-9",
	"USP45",
	"Uzi"
  }

function onClick(player, weapons)
	--return player.Backpack:findFirstChild(weapons) and player.Character:findFirstChild(weapons)
	if player.Backpack:findFirstChild(weapons) then
		weapons:Destroy()
	elseif player.Character:findFirstChild(weapons) then
			weapons:Destroy()
		end
	end

script.Parent.MouseButton1Down:connect(onClick)

1 answer

✓ Accepted answer

Because the callback function to MouseButton1Down passes two arguments: integer x and integer y, which represent the coordinates of the mouse. You took these arguments with player and weapons in your onClick function. This is unnecessary, since you already have a reference to the local player and to the weapons table.

You're also passing the weapons table to findFirstChild, which is just going to return nil. I assume you want to iterate the list of weapons to see if they exist inside the player's Backpack or Character, so for that you should use a (numeric) for loop.

-- ... code from above

script.Parent.MouseButton1Down:Connect(function()
	-- Create variables for easier reference
	local character = player.Character 
	local backpack = plater.Backpack

	-- Iterate weapons list via index
	for n = 1, #weapons do
		local weaponName = weapons[n]

		-- Find weapon in container
		local charWeapon = character:FindFirstChild(weaponName)
		local backWeapon = backpack:FindFirstChild(weaponName)

		-- Remove them if they exist.
		if charWeapon then charWeapon:Destroy() end
		if backWeapon then backWeapon:Destroy() end
	end
end)

You should also use Connect over connect (same goes for disconnect, and other event signal methods). ROBLOX has deprecated the use of them and they've since been replaced. Same goes for findFirstChild, which should be FindFirstChild.

This discussion is preserved as it was. New answers, comments, and votes are closed.