Click to get tool

So I made this thing where if you click the part you get a tool. I made it debounce so you can only get it once. The problem is that only one player can obtain the tool, not others. What do I do to fix this?

Script:

local ToolNames = {"Sleeping Set"}
local Storage = game:GetService("ServerStorage")

local debounce = false


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

ClickDetector.MouseClick:connect(function(Player)
	if Player and Player.Character and debounce == false then
		debounce = true
		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)

Thanks!

5 Likes

Is this on a client or server side script?

Is there any other tool named tool or is there any error in the dev console?

You should have it in a local script so that each player has their own script to run instead of a script.

They Shouldn’t they are trying to acsess server storage from a client script (impossible) if they arent using a remote event which they arent

local ToolNames = {"Sleeping Set"}
local Storage = game:GetService("ServerStorage")

local debounce = false


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

ClickDetector.MouseClick:connect(function(Player)
	if Player and Player.Character and debounce == false then
		debounce = true
		local Backpack = Player:WaitForChild("Backpack")
		for i,v in pairs(ToolNames) do
			local Tool = Storage:FindFirstChild(v)
			if Tool then
				Tool:clone().Parent = Backpack
else
warn(Tool.Name..' NOT IN STORAGE')
			end
		end
	end
end)

Use ipairs/pairs loop! ;D

It does not work. No errors. :confused:

I am getting this error

You don’t need a debounce, you can just check if the player has the tool already. If he does not have it, give it to him. Is that what you are trying to achieve?

ClickDetector is nil, try this

local ToolNames = {"Sleeping Set"}
local Storage = game:GetService("ServerStorage")

local debounce = false


local Part = script.Parent
local ClickDetector = Instance.new('ClickDetector',Part)

ClickDetector.MouseClick:connect(function(Player)
	if Player and Player.Character and debounce == false then
		debounce = true
		local Backpack = Player:WaitForChild("Backpack")
		for i,v in pairs(ToolNames) do
			local Tool = Storage:FindFirstChild(v)
			if Tool then
				Tool:clone().Parent = Backpack
else
warn(Tool.Name..' NOT IN STORAGE')
			end
		end
	end
end)

Yes.
30 charsssssssssssssssssssssssssssssssssss

Still does not work.
30 charrr

local ToolNames = {"Sleeping Set"}
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
			if(not Player.Character:FindFirstChild(ToolNames[i]) and not Backpack:FindFirstChild(ToolNames[i])) then
				local Tool = Storage:FindFirstChild(ToolNames[i])
				if Tool then
					Tool:clone().Parent = Backpack
				end
			end
		end
	end
end)

Try this. It will give the player the tool only if he does not have it.
I just checked if his character and backpack do not contain it already, instead of the debounce, so other players can use it too.

It seems that you have forgotten to set debounce to false at the end of the script.

You are meant to set debounce to true after checking if it is false

This should work

local ToolNames = {"Sleeping Set"}
local Storage = game:GetService("ServerStorage")

local debounce = false


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

ClickDetector.MouseClick:connect(function(Player)
	if Player and Player.Character and debounce == false then
		debounce = true
		local Backpack = Player:WaitForChild("Backpack")
		for i,v in pairs(ToolNames) do
			local Tool = Storage:FindFirstChild(v)
			if Tool and Player.Backpack:FindFirstChild(v) == nil and Player.Character:FindFirstChild(v) == nil then
				Tool:clone().Parent = Backpack
else
warn(Tool.Name..' NOT IN STORAGE')
			end
		end
	end
end)

No, he wants the player to get it once, I removed the debounce, he want the player to get the tool only when he doesn’t have it.

If you set the debounce to false, the player will be able to get it again. But right now, when you set the debounce to only false, no other players can get the tool.
He said his script worked for him only for one player.

You have set the debounce to true to check if the player has the tool and to stop it running 2 times at once :white_check_mark:

But have you set it to false when the script has finished so that another player can get the tool?

But why do you need a debounce? He wants the player to get the tool only once, if he clicks it again (with setting back debounce), he will get the tool again. And if he won’t change debounce back. No other players can get the tool.
He should just check if the player doesn’t have the tool. Like I did:

And also, if he didn’t want it to repeat, he could have just disconnected the function. So it will just never run again.

Actually, the script cannot check if there is a tool in the backpack, I think you’ll need to use a remote event so when you click the part then it fires the client and checks whether the player have the following tool.

Yeah and the script cannot clone a tool from the Serverscript to the player’s backpack, you’ll need to use a local script to handle this too.

1 Like