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)