Keybind searching function improvement request!

Hi everyone! Not a big code review for you, but I have a small question for an improvement.

I have a function (shown below) that goes through keybinds in a table, based on an input (Enum.KeyCode, or Enum.UserInputType), and returns the keybind name when it finds a match!

Main issue in the function, being that every time you press any key, it will loop through all keybinds (which might add up in the future)
Questions that I have:

  • 1: Is there a way I can improve the code, so it doesn’t loop through all keybinds every time?
    • 1.2: Previous idea I had was to cache the inputs, but the problem with that, is that I’ll still loop through all keybinds when pressing other keys
  • 2: Should I just not mind these small issues? Because as I know, Roblox is fast, and optimizations for a thing that happens only once (not every frame) might be worthless

Here is the code. It is located in a ModuleScript with some other functions for keybinds
This is the only function that loops through anything, which I really don’t like

inputToKeybind = function(input: EnumItem)
	for keybind, keybindInput in pairs(PData.plr.keybinds) do
		if keybindInput ~= input then continue end
		return keybind
	end
	return nil
end;

Note: “PData” is the ModuleScript. “PData.plr.keybinds” is the table with keybinds. Example table:
{[“Jump”] = Enum.KeyCode.Space}

This is my first time posting anything on the devforums, so if this is a worthless request, just say so!
I might start posting frequently here, with questions for improvement, that aren’t like this. I just wanted to post something for a long time now! :+1:
Thank you so much! :slight_smile:

1 Like

Instead of mapping from keybind to input you could map from input to keybind so you can directly access the keybind given an input without having to loop through all keybinds. Let me show you how to do it

keybinds = {
    [Enum.KeyCode.Space] = "Jump"
}

inputToKeybind = function(input: EnumItem)
    return keybinds[input]
end

So basically inputToKeybind function will return the keybind directly for a given input, or nil if the input is not found in the table

2 Likes

Oh wait. That’s actually smart. I also had a function called “keybindToInput”, but I just found out, I never used it anywhere!
WAIT. Also your way prevents multiple keybinds to be mapped to the same key!!

Thank you so much! Wow

Thank you. I have been learning Lua for 2 years so I might help someone one way or another.
Goodluck in your game.

1 Like

I’m also somewhat skilled. I just used {[keybind name] = [keybind input]} format for a long time now (since I started scripting in Roblox Studio. 7+ months now), and recently started making a new game, because the previous one is becoming really messed up with all the scripts.
Also it was a fan-made game of another game that I’m awaiting to come out, so it will die when that comes out.
Me and my dev team already planned a lot of stuff for the new game!

By the way, before scripting in Roblox, I scripted A LOT in Python and some time in Java. So everything is easy for me. But when I think that something works well, I might not move away from it for an extremely long time!

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