Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

The difference between UserInputService and ContextActionService?

What is the difference between the two, and in what situations is it best to use one over the other?

2 answers

✓ Accepted answer

The UserInputService can bind functions to user input via events such as "InputBegan", for example:

local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
	local char = plr.Character
	local hum = char.Humanoid

function superJump(inputObject, GPE)
	if inputObject.KeyCode == Enum.KeyCode.U then
		hum.JumpPower = 150
		print("Super jump!")
	end
end

UIS.InputBegan:connect(superJump)

As for the ContextActionService, it can bind a SET of inputs to a function, additionally it can create GUI buttons on mobile for compatibility.

For example:

local CAS = game:GetService("ContextActionService")
	local plr = game.Players.LocalPlayer
	local char = plr.Character
	local hum = char.Humanoid

function speedUp(actionName, userInputState, inputObject)
	if userInputState == Enum.UserInputState.Begin then
		print("RUNNING!!!")
		hum.WalkSpeed = 100
	end
end

CAS:BindAction("speedingUp", speedUp, false, Enum.KeyCode.Y)

Also, a function bound through BindAction can be fired on all changes of the input's state, whether it began, ended, or changed.

Personally, if I were beginning keyboard input, I would go with the UserInputService for simplicity!

http://wiki.roblox.com/index.php?title=API:Class/UserInputService http://wiki.roblox.com/index.php?title=API:Class/ContextActionService

2 comments

0

so with userinputservice, i use a specific input value (lets say the letter y) to do something for me, its the same with contextaction but it can unbind y under a condition, or bind something else under a condition?

0

Basically, I don't know too much about the CAS

ContextActionService can bind/unbind keybinds. UserInputService does the exact same thing as ContextActionService, exept that it cannot bind/unbind.

If you have a keyboard-press activated door, it's best to use ContextActionService, since you can bind a button to the door when you're close to it, instead of all the time.

If you want to have a quick respawn button, it's best to use UserInputService since you probably want to have the quick respawn feature active all the time.

http://wiki.roblox.com/index.php?title=API:Class/ContextActionService http://wiki.roblox.com/index.php?title=API:Class/UserInputService

This discussion is preserved as it was. New answers, comments, and votes are closed.