Player only allowed one tool

Hello, I have made a script that when a player clicks on a part it checks if a player already has a certain tool.

Everything works until the tool is not in the players backpack.
This is the script I used.
Script Below:

local tool = game.ServerStorage.Bat
local clone = tool:Clone()
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
    	if plr.Character:FindFirstChild("Humanoid") then
    	local player = game.Players:GetPlayerFromCharacter(plr.Character)
    	if plr.Backpack.Add and plr.Backpack.Add:IsA("Tool") then
    		print(plr.Name.." You already have a weapon!") -- Player already has a tool
    		else -- When player does not have a tool
    			script.Parent:Destroy()
    		if Clone.Parent ~= plr.Backpack then
    		Clone.Parent = plr.Backpack
    	end	
    	end
    	end
    end)

I have tried other methods but they do not work. Any help is appreciated!

2 Likes

Have you tried to do Player.Backpack:FindFirstChild("Tool") ?

1 Like

Can you tell us where the script is located?

The script is located in the part where you click.
Here is an image of where the script is located in:
image

Hi @soccervr,

A tool can be in two sites,
a) if the player is holding the tool then it’s in the player humanoid
b) if the player is not holding the tool is in the player backpack (game.Players[player.Name].Backpack[toolname]

The tool can’t be equipped as a player cant use a clickdetector while equipping a tool.

I tried to fix a little the script and this is with what I came:

local tool = game.ServerStorage.Bat
local clone = tool:Clone()
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
    	if plr.Character:FindFirstChild("Humanoid") then
    	local player = game.Players:GetPlayerFromCharacter(plr.Character)
    	if plr.Backpack:FindFirstChild("Tool Name") and plr.Backpack["Tool Name"] then
    		print(plr.Name.." You already have a weapon!") -- Player already has a tool
    		else -- When player does not have a tool
    		script.Parent:Destroy()
    		if Clone.Parent ~= plr.Backpack then
    		Clone.Parent = plr.Backpack
    	end	
    	end
    	end
    end)
3 Likes