How do I fix my keybind script?

Hey all. So i have this Keybind tool that allows to me set up keybindings for multiple objects. So its meant to have a feature where you can bind, and unbind keys. Sometimes when you have more than one binded object and you try to unbind one of them, it just gets you to create another keybind instead of unbinding. I’ve tried fixing this my self, and i just couldn’t seem to get it to work. Help will be much appreciated as always!

here is a video of the issue: https://youtu.be/mZuBAN0VvkY

and here is the script:

Tool = script.Parent
player = game:GetService("Players").LocalPlayer
mouse = nil

Event = script.Parent:WaitForChild("Event")

Keybinds = Tool:WaitForChild("Keybinds")

RunService = game:GetService("RunService")
UIS = game:GetService("UserInputService")

equipped = false

GUI = script:WaitForChild("KeybindGui")

InUse = false

SelectionBox = Instance.new("SelectionBox")
SelectionBox.LineThickness = 0.1
SelectionBox.Color3 = Color3.fromRGB(0, 255, 0)
SelectionBox.Parent = Tool

function CreateKeyBind(Object, Key)
	local NewKeybindObject = Instance.new("ObjectValue")
	NewKeybindObject.Name = Key.Name
	NewKeybindObject.Value = Object
	NewKeybindObject.Parent = Keybinds
end

RunService.RenderStepped:Connect(function()
	if mouse and equipped then
		if mouse.Target and mouse.Target:IsA("BasePart") and mouse.Target.Locked == false then
			local IsBindable = mouse.Target:FindFirstChild("Bindable_")
			local OwnerCheck = mouse.Target:FindFirstChild(player.Name)
			if IsBindable and OwnerCheck then
				local Part = mouse.Target
				SelectionBox.Adornee = Part
			else
				SelectionBox.Adornee = nil
			end
		else
			SelectionBox.Adornee = nil
		end
	else
		SelectionBox.Adornee = nil
	end
end)


UIS.InputBegan:connect(function(Input)
	local MouseInput = Input.UserInputType
	local KeyCode = Input.KeyCode
	
	if equipped then
		if InUse then
			if KeyCode.Name ~= "Unknown" and KeyCode.Name ~= "Escape" and KeyCode.Name ~= "One" and KeyCode.Name ~= "Two" then
				InUse = false
				
				local KeyGui = GUI:Clone()
				KeyGui.TextLabel.Text = KeyCode.Name
				KeyGui.Name = "KeyText"
				KeyGui.Parent = script
				
				local CleanCheck = script.GuiCheck:Clone()
				CleanCheck.Parent = KeyGui
				CleanCheck.Disabled = false
				
				CreateKeyBind(GUI.Adornee, KeyCode)
				
				GUI.Enabled = false
				GUI.Adornee = nil
			end
		else
			if KeyCode.Name ~= "Unknown" and KeyCode.Name ~= "Escape" and KeyCode.Name ~= "One" and KeyCode.Name ~= "Two" then
				local KeybindedObjects = Keybinds:GetChildren()
				for i, child in ipairs(KeybindedObjects) do
					InUse = false
					if child.Name == KeyCode.Name then
						Event:FireServer(child.Value)
					end
				end
			end
		end
	end
end)


Tool.Activated:Connect(function()
	if SelectionBox.Adornee and InUse == false then
		local Check = script:FindFirstChild("KeyText")
		if Check then
			if Check.Adornee ~= mouse.Target then
				InUse = true
				local Part = mouse.Target
				GUI.Enabled = true
				GUI.Adornee = Part
			else
				local Key = Keybinds:FindFirstChild(Check.TextLabel.Text)
				if Key then
					Key:Destroy()
					Check:Destroy()
					InUse = false
					GUI.Enabled = false
					GUI.Adornee = nil
				end
			end
		else
			InUse = true
			local Part = mouse.Target
			GUI.Enabled = true
			GUI.Adornee = Part
		end
	else
		InUse = false
		GUI.Enabled = false
		GUI.Adornee = nil
	end
end)

Tool.Equipped:Connect(function(playerMouse)
	mouse = playerMouse
	equipped = true
end)

Tool.Unequipped:Connect(function()
	equipped = false
end)

Whats the CleanCheck for on line 64?

The only advice i would give is instead of storing the Keycode UI in the Tool Store it in the part then add a check when you click on the part it will check if there was a keycode set to that part and make it delete all old data for that part also i would max only having one keycode per part just

1 Like

just a script that deletes the BillboardGui if the adornee goes nil

Keybinds = script.Parent.Parent.Parent:WaitForChild("Keybinds")
de = false

while true do
	wait(0.2)
	if script.Parent and script.Parent.Adornee == nil then
		local Key = Keybinds:FindFirstChild(script.Parent.TextLabel.Text)
		if Key and de == false then
			de = true
			Key:Destroy()
			script.Parent:Destroy()
		end
	end
end