How would i make this tool giver script only usable once?

Trying to make this script limit the player to only getting one of the tools.
Any help?

local tool = game.ServerStorage["Pickaxe"]
local giver = script.Parent
local canGive = false

local function GiveTool (player)
	if canGive == false then
		local clone = tool:Clone()
		clone.Parent = player.Backpack
		wait(1)
		canGive = false
	end
end

giver.ClickDetector.MouseClick:Connect(function(player)
	GiveTool(player)
end)

Instead do:

giver.ClickDetector.MouseClick:Connect(GiveTool)

Also.i noticed there is a space between your function, narrow it.

1 Like
local tool = game.ServerStorage["Pickaxe"]
local giver = script.Parent
local canGive = false

local function GiveTool (player)
	if canGive == true then
		local clone = tool:Clone()
		clone.Parent = player.Backpack
		wait(1)
		canGive = false
	end
end

giver.ClickDetector.MouseClick:Connect(function(player)
	GiveTool(player)
end)

all you have to do hear is swap the “canGive == false” with “canGive == true” in your if statement, and hopefully it should work

1 Like

also you might not need a wait

1 Like
local tool = game.ServerStorage["Pickaxe"]
local giver = script.Parent
local canGive = false

local function GiveTool (player)
	if canGive == true then
		local clone = tool:Clone()
		clone.Parent = player.Backpack
		canGive = false
	end
end

giver.ClickDetector.MouseClick:Connect(GiveTool)

fixed

Hmmm… I seem to be getting 'Infinite yield possible on ‘Workspace.DelovilleMiner:WaitForChild(“ForceField”)’" when i try this
Not sure how the ForceField is related but i have it disabled through a script

If it’s not related at all and you don’t need it,then you should probably just delete the line of code

You can also use attributes for this script.

image
This ended up working fine for anybody wondering, thanks for the replies

1 Like

Although this topic is already solved, there is another solution which is disconnecting the connection; something like this would work. This is pseudocode

local connection

connection = giver.ClickDetector.MouseClick:Connect(function() 
      tool:Clone().Parent = ["PARENT"]
      connection:Disconnect()
end)
1 Like