Help with UserInputService and ContextActionService

So basically, I am using ContextActionService to bind a function, but I want it to do something WHEN I press it and something else WHEN I let the key go.

Sadly it seems buggy:

	local Cooldown = Moves[actionName].Frame:WaitForChild("Cooldown")
	Cooldown.Size = UDim2.new(1, 0, 1, 0)
	
	local Amount = #Connection + 1
	Connection[Amount] = Services.UserInputService.InputEnded:Connect(function(Input)
		if Input == inputObject then
			Services.TweenService:Create(Cooldown, TweenInfo.new(1), {Size = UDim2.new(1, 0, 0, 0)}):Play()
			Connection[Amount]:Disconnect()
			Connection[Amount] = nil
		end
	end)

Any ideas why?

1 Like

If your InputObject variable is a KeyCode enum, you should use Input.KeyCode instead.

Its can be multiple different ones

Can you explain that in further detail?

I am binding multiple different binds with that same function.

You’re using UserInputService incorrectly, InputObject | Documentation - Roblox Creator Hub

I don’t see how that would affect anything. You should still check if Input.KeyCode is equal to the inputObject.

	Connection[Amount] = Services.UserInputService.InputEnded:Connect(function(Input)
		if Input.KeyCode == inputObject then
			Services.TweenService:Create(Cooldown, TweenInfo.new(1), {Size = UDim2.new(1, 0, 0, 0)}):Play()
			Connection[Amount]:Disconnect()
			Connection[Amount] = nil
		end
	end)

of course, if inputObject is not a KeyCode, you should also use inputObject.KeyCode.

The problem is that there are multiple binds, so if I use two binds at once, either one of the binds get disconnected, or altered

The reason this is happening is most likely due to bad indexing. You can do something a little simpler, I would assume. I would also like to ask why you are storing all of your connections in a table?

You should be using ContextActionService:BindAction for this. This ensures your game won’t double-bind the same key to multiple things, and is generally a better pattern than UserinputService’s InputEnded.

local ContextActionService = game:GetService("ContextActionService")
local ACTION_NAME = "OpenDoor" -- Change this to something unique that makes sense
local INPUTS = { Enum.KeyCode.E } -- Add other KeyCode you see fit here

local function handleAction(actionName, inputState, inputObject)
    if actionName == ACTION_NAME then
        if inputState == Enum.UserInputState.Begin then
            -- The key is pressed, mouse button down, etc...
        elseif inputState == Enum.UserInputState.End then
            -- The key is released, mouse button released, etc...
        end
    end
end

-- When the action becomes relevant:
ContextActionService:BindAction(ACTION_NAME, handleAction, false, unpack(INPUTS))
-- When the action is no longer relevant:
ContextActionService:UnbindAction(ACTION_NAME)

To answer your question, InputObject.UserInputState is the property you want to check, much like in my example above.

1 Like

This should be a local script

@OminousVibes0 When UserInputService you can type a variable…

local uis = game:GetService(“UserImputService”)

then you can connect the key maybe for example your Key Is M

Type this script under the variable

function onKeyPress(action, UserInputState, inputobject)
if userInputState == Enum.UserInputSate.Begin then
– type in here what you want the script to do if you want it to something in the output type
print(“M Was Pressed”)
–after type what you want the script to do type this in your script

end
end

game.ConextActionService:BindAction(“keyPressSpecialName”, onKeyPress, true,Enum.KeyCode.M)
– What You Want Your Keycode to be
game.ConextActionServiceSetPostion:BindAction(“keyPressSpecialName”, UDim2.new(0.01,0,.6,0)
– This line sets the position of it to near the jump button
game.ConextActionService:SetImage(“keyPressSpecialName”, " – Paste the image link here")

If you dont understand what i said watch this video

1 Like

ight I fixed it, sorry for the inconvenience