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!
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)
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.
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?
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)
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)