Custom keybind actions - Which method is better?

This isn’t a “how to make custom keybinds” question, this is a “Which of the listed methods is better” question;

A. have a dictionary, where:

  • Key = keybind,
  • Value = bindable event

and then whenever a key is pressed, loop through that dictionary and see if the key pressed is equal to one of the keys in the dictionary, if yes, fire the event

an example of this would be:

local keyActions = {
   ["LeftControl"] = BindableEventInstance,
   -- and so on
}

local function OnInput(input: InputObject)
   for key, bindableEvent in keyActions do
     if input.KeyCode == key then bindableEvent:Fire() end
     -- can someone tell me if that if statement is written correctly lol
   end
end

this would result in all action invoking to be in the same script, while the action handling can stay independent


B. independently check the key in an action-handling script

rather than having a seperate script that invokes actions, all the actions would already be checking if the key is correct, for example;

local playerKeybinds = require(something)

local function OnInput(input: InputObject)
   if input.KeyCode == playerKeybinds[actionName] then
     -- perform actions, yes i know the if statement above will error,
     -- but it's just an example
   end
end
1 Like

You could do:

local function OnInput(input: InputObject)
   keyActions[input.Name]:Fire()
end

And in this case, the second method is better. BindableEvents are insecure and can cause memory leaks so it’s best to use the second method.

1 Like

how?

also why do i not know this?

1 Like

BindableEvents are insecure because the client can use it to activate actions without pressing keys. This can lead to a massive bot problem in the future.

2 Likes

society if not everything in roblox was exploitable:

1 Like

It’s not only with Roblox. It’s with every game engine and game. Roblox has provided us with a useful source, and it’s our task to use it responsibly. BindableEvents, for example, are useful for parallel luaU. They are useful for server-server communication as well.

Although, it’s unlikely it will be exploited much and I’m not 100% sure about the memory leaks as well but personally, the best way is just to use an external module script with all keybinds and action. It’s simply more convinient.

2 Likes