InputBegan not working as intended when updating module value

  1. What do you want to achieve? I want to make a keybind UI work so it changes a local module to the new keybind when players input the key in.

  2. What is the issue? When you press the new key, it doesn’t print out anything or work. There are no specific errors.

  3. What solutions have you tried so far? Looked through multiple topics but none of them seem similar to mine, I also tried putting the input script seperately with one of the keybinds but that also didn’t work.

I have a key settings UI which players can use to change their keys to their liking, the script changes the module value however when you try to press that new key, it doesn’t seem to work neither does it seem to work when you use the original key.

The module value changes however the input began script doesn’t seem to acknowledge the new value.

Input Script:

   for i, toolModule in pairs(toolModules) do
        local listener = UIS.InputBegan:Connect(function(key, gpe)
            if gpe then return end
            for j, hotkey in pairs(toolModule.HotKeys) do
                if key.KeyCode == hotkey.HotKey then
                    print("works")
                end
            end
        end)
    end

Keybind change script:

for i, keybind in pairs(plr:WaitForChild("Keybinds"):GetChildren()) do
		if string.find(keybind.Name,"Left Dribble") then
			toolModules["Dribble"].HotKeys[keybind.Name].HotKey = "Enum.KeyCode."..keybind.Value
		end
		keybind:GetPropertyChangedSignal("Value"):Connect(function()
			if string.find(keybind.Name,"Left Dribble") then
				toolModules["Dribble"].HotKeys[keybind.Name].HotKey = "Enum.KeyCode."..keybind.Value
			end
		end)
	end 

Both of these scripts are in a local script,

Corrosponding module script - located in ReplicatedStorage.

local dribbleModule = {}

dribbleModule.HotKeys = {
	["Left Dribble"] = {HotKey = Enum.KeyCode.Z}
}

return dribbleModule

You are setting the HotKey to a string value, not an Enum. If you change this to Enum.KeyCode[keybind.Value] it should work.

On a side note, you no longer need to use the pairs() function to iterate through a table.

for i, v in t do
   -- works
end
2 Likes

Firstly, in the keybind change script, the line toolModules["Dribble"].HotKeys[keybind.Name].HotKey = "Enum.KeyCode."..keybind.Value is assigning the value of "Enum.KeyCode."..keybind.Value to HotKey , which means it’s storing a string representation of the key code, rather than the actual key code. This means that when you compare the input in the input script, it won’t match the stored value. To fix this, you should use the Enum.KeyCode[keybind.Value] syntax to convert the string representation of the key code back into the actual key code:

for i, keybind in pairs(plr:WaitForChild("Keybinds"):GetChildren()) do
		if string.find(keybind.Name,"Left Dribble") then
			toolModules["Dribble"].HotKeys[keybind.Name].HotKey = Enum.KeyCode[keybind.Value]
		end
		keybind:GetPropertyChangedSignal("Value"):Connect(function()
			if string.find(keybind.Name,"Left Dribble") then
				toolModules["Dribble"].HotKeys[keybind.Name].HotKey = Enum.KeyCode[keybind.Value]
			end
		end)
	end

Secondly, in the input script, the hotkey.HotKey is a key code, but the key.KeyCode is an enum value, so the comparison if key.KeyCode == hotkey.HotKey won’t match. You should use the key.KeyCode.Value == hotkey.HotKey.Value instead:

for i, toolModule in pairs(toolModules) do
        local listener = UIS.InputBegan:Connect(function(key, gpe)
            if gpe then return end
            for j, hotkey in pairs(toolModule.HotKeys) do
                if key.KeyCode.Value == hotkey.HotKey.Value then
                    print("works")
                end
            end
        end)
    end

With these changes, the keybind change script should correctly update the HotKey values in the module, and the input script should correctly detect the key presses and print “works” when the correct key is pressed.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.