A way to find a specific element in a table?

i asked this in a different place, and, i got a pretty complicated answer. i’m thankful for it, but, i don’t think it’s necessary for what i need.

let’s say, i have a table with a few buttons, like so;

local buttongroup = {Enum.KeyCode.X, Enum.KeyCode.Y, Enum.KeyCode.Z}

i want it so that when i press a button, it checks for the table to contain that same button. if so, it’ll do stuff. if not, then, you’re screwed

is there a simple way to achieve this? i feel like i’m overthinking this a lot.

ContextActionService allows you to bind/unbind actions to one or multiple input types at once (i.e. multiple buttons at once):
https://www.robloxdev.com/api-reference/class/ContextActionService

I would look into that rather than what you are trying to do right now, because this question smells like you are having an XY problem.

4 Likes

I’d recommend this too. If OP still wants to do what they’re doing currently they could make a table that looks like this:

keycodeTable = {Enum.KeyCode.X = true, Enum.KeyCode.Y = true, Enum.KeyCode.Z = true}

To check if it contains a keycode do:

if keycodeTable[keyCode] == true then
– do stuff
end

1 Like

i tried this out, though, as much as i want to go “this is EXACTLY what i want” i don’t know if it is?
i’m not entirely sure how to use it either, even looking at the wiki page. when i did try it out though, it was a bit messy.

for example, three buttons can open up the menu. i can only get one button to work and the menu only stays for how long you hold down the button, which isnt what i want. i’d want the menu up after you press it, and stay up until you press it again, you know?

i’ll try looking into it some other time, though, thank you!

If you are using ContextActionService::BindAction, you bind a function like this:

function(actionName, inputState, inputObject)
   ...
end

You don’t need to use all the parameters, but suppose you want to make it so that the menu only toggles when you first press a button (not when releasing), you’d check if the inputState is equal to Begin, so the function you feed to BindAction is:

function(_, inputState, _)
   if inputState == Enum.UserInputState.Begin then
      toggleMenu()
   end
end

To feed multiple buttons, you just add more keycodes to the end of your BindAction call. For example:

ContextActionService:BindAction(
   "MyMenuAction",
   function(_, inputState, _)
      if inputState == Enum.UserInputState.Begin then
         toggleMenu()
      end
   end,
   false,
   Enum.KeyCode.X,
   Enum.KeyCode.Y,
   Enum.KeyCode.Z
)

This is really the superior option. Don’t convince yourself having a table of inputs and doing it via UserInputService is a better alternative – it’s not.

2 Likes

oooh, that makes more sense!
could i ask what the heck touchbutton is though

also, the way i have my controls stored, since they’re used all over in my scripts for various reasons, is in an array. so, with that said, is there any way i can assign an array of buttons to an action?

Just leave that bool createTouchButton as false always, it’s kinda bad API design from Roblox’s side, I don’t really know anyone that uses that.

You can use unpack to have your button table as a trailing argument list:

ContextActionService:BindAction(
   "MyMenuAction",
   function(_, inputState, _)
      if inputState == Enum.UserInputState.Begin then
         toggleMenu()
      end
   end,
   false,
   unpack(buttongroup) -- 'buttongroup' being your table
)
1 Like

Since ContextActionService is meant for quick cross-platform support, it seems pretty straightforward - touch devices normally doesn’t have a keyboard to use, so having ContextActionService create a touch button helps mobile / tablet users without a physical keyboard.

Setting createTouchButton to true creates a button in the same style as the touch jump button - you can define the text, image and other properties about it using other of the ContextActionService methods.

Here’s a screenshot from looking on the interwebs, showing it being used (see “Use Skill”)

1 Like

The style of the jump button has changed and last I checked they didn’t update the CAS button backgrounds. That might have updated though.

Another issue is that with even 2 or a few more buttons, the ones added later will start going off-screen very quickly, there doesn’t seem to be any kind of smart mechanism for how these are positioned on-screen.

I don’t recommend using them for that reason and instead defining your own buttons and connecting those to the same actions. That way you don’t have a dependency on CAS creating them for you first, then having to fetch them through CAS, and adjusting position/size etc. It’s also convenient if you are able to unbind CAS calls without the buttons vanishing (i.e. if you want to disable the menu at certain times and grey out the button responsible for it).

2 Likes