is there any way that players can only get one tool when they touch the click detector tool giver ? Because if you spam click the click detector tool giver it will give you a bunch of the same tools.
1 Like
Use Instance:FindFirstChild(ChildName: string) to check if they already have the tool in their character, and in their backpack.
1 Like
Before giving the player the tool, check if they have a copy in their character or their backpack. For example:
local Tool = game:GetService("ServerStorage").Tool
local ToolName = Tool and Tool.Name
ClickDetector.MouseClick:Connect(function(Plr)
if not (Plr.Backpack:FindFirstChild(ToolName) or (Plr.Character and Plr.Character:FindFirstChild(ToolName))) then
Tool:Clone().Parent = Plr.Backpack
end
end)
Fair amount of redundant code here.
local Tool = game:GetService("ServerStorage").Tool --Errors if 'Tool' isn't a valid child of the 'ServerStorage' container.
local ToolName = Tool and Tool.Name --Tool is always non-nil (an 'Instance' object) if this line is reached.
--Avoid declaring unnecessary constant upvalues, that is, immutated non-local variables as they cause increased memory consumption when they are captured by closures.
ClickDetector.MouseClick:Connect(function(Plr)
--Edge case but 'Backpack' might be 'nil'.
local Char = Plr.Character
local Bp = Plr:FindFirstChildOfClass("Backpack")
if not (Char and Bp) then return end
if (Char:FindFirstChild(Tool.Name) or Bp:FindFirstChild(Tool.Name)) then return end --Index the 'Tool' object's 'Name' property.
Tool:Clone().Parent = Bp --Can use reference defined earlier.
end)