Cloning issue with keyboard button

Hello all,

I am completely aware that I am not doing something right in my script, at the moment I am just practicing to better understand how to use UserInputService for simple things.

I am trying to script my key on my keyboard to clone a tool from ReplicatedStorage and then when the key is pressed again, to remove it. Maybe this is a less efficient way to do it but if possible I would still like to know a fix for it to understand why it’s not working.

local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local rs = game:GetService("ReplicatedStorage")
local rod = rs:WaitForChild("Gons Rod"):Clone()


local debounce = false


UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Z then
		if debounce == false then
			rod:Clone()
			rod.Parent = plr.Backpack
			debounce = true
		end
	end
end)

UIS.InputBegan:Connect(function(input)
	if debounce == true and input.KeyCode == Enum.KeyCode.Z then
		rod:Remove()
		wait()
		debounce = false
	end
end)

What’s specifically not working?

I also suggest using task.wait() instead of wait() and having both if statements in one connection, but make the second if statement elseif instead. Also use Destroy() instead of Remove() (also what’s with the wait() after that remove)

Just looked at it again and the problem is that although you clone the rod, you never give the clone a variable that you can use to access the clone again. Instead, you clone the rod and afterwards you just set the original rod’s parent to the player’s backpack, meaning that the cloned rod’s parent is nil.

If the first if statement isn’t true, then the script will run to the next if statement, being the elseif and if that’s true then it will run the code afterwards, if not, then that section of the script will stop running until the connection is fired again. (:Connect(function()))

Using this instead should make it work.

local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local rs = game:GetService("ReplicatedStorage")
local rod = rs:WaitForChild("Gons Rod"):Clone()

local debounce = false
local clonedRod -- the variable that represents the rod that is cloned.

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Z then
		if debounce == false then
			clonedRod = rod:Clone() 
			clonedRod.Parent = plr.Backpack -- now the clone's parent isn't nil since we set *it* to a variable, and the original rod will remain where it originally was.
			debounce = true
		end
	elseif debounce == true and input.KeyCode == Enum.KeyCode.Z then
		clonedRod:Destroy() -- the cloned rod is now deleted, and the original is in the same place meaning you can do the same thing again.
		debounce = false
	end
end)

Though I haven’t touched anything with the script yet, isn’t the clone rod line 4?

Oh I didn’t even notice that

yeah it is but even so in the original code it wouldn’t work so I suggest just deleting it.

Well, the rod does clone and does move to my inventory and the one in RS exists, but when pressed again after updating the script, the clonedrod will not destroy.

This should work, but it didn’t work because in my version I was checking if the same thing was true as before. Since the Z key was hit and the first condition was true, it would never run the second condition since the first was always gonna be true. Here I fixed that by dividing the two scenarios inside of one statement that checks if the Z key was hit.

local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local rs = game:GetService("ReplicatedStorage")
local rod = rs:WaitForChild("Gons Rod")

local debounce = false
local clonedRod -- the variable that represents the rod that is cloned.

UIS.InputBegan:Connect(function(input)
	print(debounce)
	if input.KeyCode == Enum.KeyCode.Z then
		if debounce == false then
			clonedRod = rod:Clone() 
			clonedRod.Parent = plr.Backpack -- now the clone's parent isn't nil since we set *it* to a variable, and the original rod will remain where it originally was.
			debounce = true
		elseif debounce == true then
			clonedRod:Destroy() -- the cloned rod is now deleted, and the original is in the same place meaning you can do the same thing again.
			debounce = false
		end
	end
end)