I clone a tool and i get 3-10 of them not just 1

Hello,

What i want is if the player press the B key they get a glider and can glide down to the map

When i press B i get 3 sometimes 10 tool not just one
Video: https://gyazo.com/cc29aa8af847bf4d26d018ca3e469142

Script(LocalScript):

lobbyPart.Touched:Connect(function()
	launchButton.Visible = true
	UserInputService.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.B then
			local newGlider = glider:Clone()
			newGlider.Parent = player.Backpack
		end
	end)
end)
1 Like

It might be because you have no debounce?

1 Like

Solution Explained:
I just added a check to not run the touch if the player already has glider. and I disconnected the button press once its pressed once.
(IMPORTANT UPDATE, I also added a debounce.)

local debounce = false
	lobbyPart.Touched:Connect(function()
		if debounce == false and not player.Backpack:FindFirstChild(glider.Name) then
			debounce = true
			local GliderInput
			launchButton.Visible = true
			GliderInput = UserInputService.InputBegan:Connect(function(input)
				if input.KeyCode == Enum.KeyCode.B then
					local newGlider = glider:Clone()
					newGlider.Parent = player.Backpack
					debounce = false
					GliderInput:Disconnect()
				end
			end)
		end
	end)

Extra tip:
This is a solution to your post, but listen for any extra advice others may give as well!

I don’t know exactly what your goal is, but this should fix the issue you posted about.
As a developer though I must tell you there are probably better ways to do this.
Good luck!

1 Like

I was going to suggest that you add a check to determine if the player has already been awarded a glider. You don’t need the debounce anymore as the glider can only be awarded once anyway.

the debounce is to stop the touch from running multiple times, as he may touch the part multiple times before deciding to press the b key. and apparently in his code he wants the player to start waiting for the b key to be pressed after touching the part.

if there was no debounce he would keep searching for that B press infinitely each time the touch gets activated until it is finally pressed.

So in my example code, I disabled the debounce once the player gets the glider.

That way if for some reason the player looses the glider, the part can be touched again if the glider is not found.

A debounce is a timed delay which reduces the frequency at which a function/block of code can be executed, you’re referring to a toggle.

local uis = game:GetService("UserInputService")

local lobbyPart = workspace:WaitForChild("Part") --change to name of lobby part
local player = game.Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
local glider = nil --define glider here

local buttonConn

lobbyPart.Touched:Connect(function()
	if backpack:FindFirstChild("Glider") then --change to name of glider
		return
	end
	
	if buttonConn then
		return
	end
	
	buttonConn = uis.InputBegan:Connect(function(key, processed)
		if processed then
			return
		end
		
		if key.KeyCode == Enum.KeyCode.B then
			local gliderClone = glider:Clone()
			gliderClone.Parent = backpack
		end
	end)
end)

lobbyPart.TouchEnded:Connect(function()
	if buttonConn then
		buttonConn:Disconnect()
		buttonConn = nil
	end
end)

Anyway, this is what I’d do in order to prevent creating multiple connections between the “InputBegan” event and its connected anonymous callback function.

1 Like

looks good to me.
I was calling it a denounce because originally I was adding wait(1) but somewhere along the way I decided it wasn’t necessary once I understood what I was reading.
I’m a bit exhausted so in my reply to you I was just referring to the variable by name not by the purpose of a debounce.

Thanks for trying to educate though, I appreciate it.