Tool on click issue

There’s no point in adding a debounce to that because that script runs instantly. Debounces should be used when you want a script that yields to not run again until it has finished running.

I updated a script make sure to copy it again.

It gives the tool, but when I reset and try to get it again, it doesn’t do anything (I did use the updated version you gave me)

Change the first value from flag first True and then Flase after it gives player a tool in a script. Fixed:

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 = true
				Tool:clone().Parent = Backpack
                Flag = false
			end
		end
	end
end)
1 Like

You don’t need a debounce though. The Flag variable is completely unnessacary.

You shouldn’t use a debounce, use what @LexiDog5 suggested. A denounce makes no sense because the tool will only be given once.

1 Like

This solved the problem. It gives me one tool and still works when I respawn! Thanks for the help.

This version achieves the same thing without unneeded use of debounces.

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 and not Backpack:FindFirstChild(Tool.Name) and not Player.Character:FindFirstChild(Tool.Name) then
				Tool:clone().Parent = Backpack
			end
		end
	end
end)
1 Like

This works as intended! Thank you, it made it even more simple.

1 Like

I just made a slight update to correct an oversight I didn’t notice. I forgot that when a tool is equipped, it is parented to the player’s character. This update will check if the tool is also not in the player’s character to prevent people from being able to get 2 tools.

1 Like