Bind a key to multiple actions?

so, i was trying to use ContextActionService and bind keys for certain functions. so far, it seems good, and it works for one script, however, it seems to not work at all when i create a new bind, using the same keys. i don’t want to use different keys, as for it would be a hassle. i’m also afraid of combining it all into one script for an unorganized mess.

any help?

Are you unbinding the existing actions for those keys before trying to create a new bind using the same keys?

Also, it would be quite helpful if you edited the original post with your code for the key binds. Otherwise, we’re left guessing what your code is currently doing making it harder for us to help you.

oh-- my bad!

i’m creating new binds for the same keys, but, i want to use /both/ binds.

for example, in one script…

ContextActionService:BindAction("rightcontrol",
--[[scripting stuff....]]--
end, false, unpack(right))

and in another,

ContextActionService:BindAction("rightfight",
--[[scripting stuff....]]--
end, false, unpack(right))

So, the problem with your code is that you can only have one action binded for each key. This means that you are overriding the first binded action which is why you’re not getting the results you’re looking for. I would recommend putting the code for rightcontrol and rightfight each into their own functions so it is still organized. Then create another function that will be used when binding the keys; this function should execute both the functions for rightcontrol and rightfight.

Example code:

local function RightFight()
   -- Code for "rightfight" should go here
end

local function RightControl()
    -- Code for "rightcontrol" should go here
end

local function HandleBind()
    RightFight()
    RightControl()
end

game:GetService('ContextActionService'):BindAction('RightStuff', HandleBind, false, unpack(right))

Also, you may want to consider looking into using module scripts. Using module scripts can be quite helpful with writing clean and organized code compared to using just one huge script or multiple scripts.

4 Likes

Your issue is probably coming from not returning a ContextActionResult. The ContextActionService:BindAction() docs have a good explanation and code example for this.

okay, this helps out a lot. thank you!

i’ll reconfigure my scripts so i can keep it all in one, AND, so its still organized.

thank you so much!

1 Like