Tool on click issue

Hello! I have an issue. I got a script that gives you a tool, once you click on a part. Everything is working just fine. But there is one major issue. I want players to only get one tool per spawn, when they click the part. But each time I press the part, it gives a tool. This gives you infinite tools. Basically I want players to only get one, and when they press the part more then once, I want the script to ignore it. I hope this is a understandable description of the problem.
Here is the code:

local ToolNames = {"Item", "Item", "Item"}
local Storage = game:GetService("ServerStorage")


local Part = script.Parent
local ClickDetector = Part:WaitForChild("ClickDetector")

ClickDetector.MouseClick:connect(function(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		for i = 1, #ToolNames do
			local Tool = Storage:FindFirstChild(ToolNames[i])
			if Tool then
				Tool:clone().Parent = Backpack
			end
		end
	end
end)

It would be great if someone told me what to add in!
Thanks in advance!

You can add a debounce, like this:

local debounce = true

part.Touched:Connect(function()
if debounce == true then
debounce = false
print("Touched")
end
end)

I’m kinda unsure where i should add these lines :confused: . I’m pretty new to scripting, could you tell me which line I should add that in?

So if you want to use your script only once then add an boolean to it. Here is a fixed version:

local ToolNames = {"Item", "Item", "Item"}
local Storage = game:GetService("ServerStorage")
local Flag = false

local Part = script.Parent
local ClickDetector = Part:WaitForChild("ClickDetector")

ClickDetector.MouseClick:Connect(function(Player)
	if Player and Player.Character and Flag ~= true then
		local Backpack = Player:WaitForChild("Backpack")
		for i = 1, #ToolNames do
			local Tool = Storage:FindFirstChild(ToolNames[i])
			if Tool then
				Tool:clone().Parent = Backpack
                Flag = true
			end
		end
	end
end)
1 Like

That seems to work perfectly! Thank you so much for the help!

1 Like

You have to detect if the player already has a tool.

if backpack:FindFirstChildOfClass("Tool") then
    --Player has tool
end

Additionally, you would also have to check if the player’s character has the tool, because that’s where it goes when it’s equipped.

if backpack:FindFirstChildOfClass("Tool") and player.Character:FindFirstChildOfClass("Tool") then
    --Player really has a tool
end
1 Like

Debouncing has nothing to do with the question. The OP wants to know how to check a user’s backpack to see if the tool is there and only give it if it is not. @ArticGamerTV 's solution should work.

2 Likes

Wouldn’t this result in the tool only being able to be picked up once? He’s asking for a way to only let people have a tool in their inventory one time instead of being able to pick it up an infinite number of times which can fill up your inventory?

1 Like

He wants to be like that. Read the topic.

1 Like

I just noticed that. I can only pick the tool once and not when I respawn.

2 Likes

I did read the topic and he is saying he only wants players to have one of the same tool at a time…

The solution was to check if the player does not have the tool in their inventory, and if so, clone in the tool.

if Tool and not Backpack:FindFirstChild(Tool.Name) then
	Tool:clone().Parent = Backpack
end

(This won’t work if you have two tools with the same name)

2 Likes

I tried putting the script into the tool, but that doesn’t seem to work. It may be me, doing something wrong.

2 Likes

You need to replace this part of the script you posted in the first post here

1 Like

@ArticGamerTV added a debounce, which is what I suggested. But like @LexiDog5 said it wouldn’t work because only one player would get the tool.

2 Likes
local ToolNames = {"Item", "h", "e"}
local Storage = game:GetService("ServerStorage")


local Part = script.Parent
local ClickDetector = Part:WaitForChild("ClickDetector")

ClickDetector.MouseClick:connect(function(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		for i = 1, #ToolNames do
			local Tool = Storage:FindFirstChild(ToolNames[i])
			if Tool and not Backpack:FindFirstChild(Tool.Name) then
	Tool:clone().Parent = Backpack
end
			end
		end
	end
end)

Is this right?

1 Like

Also don’t do

if something == true then

just do

if something then

It just makes the code a bit more concise.

2 Likes

Here you go a fixed version:

local ToolNames = {"Item", "Item", "Item"}
local Storage = game:GetService("ServerStorage")
local Flag = false

local Part = script.Parent
local ClickDetector = Part:WaitForChild("ClickDetector")

ClickDetector.MouseClick:Connect(function(Player)
	if Player and Player.Character and Flag ~= true then
		local Backpack = Player:WaitForChild("Backpack")
		for i = 1, #ToolNames do
			local Tool = Storage:FindFirstChild(ToolNames[i])
			if Tool and Backpack:FindFirstChild(Tool.Name) == nil then
                Flag = false
				Tool:clone().Parent = Backpack
                Flag = true
			end
		end
	end
end)

There’s no point of a debounce if your gonna check if the player has the tool anyway.

I know but still it can help by overusing the client side. It’s going to reduce the performance.

This doesn’t give me the tool at all…