Actions, a ContextActionService alternative

Actions

Overview

Actions is a module I’ve put together that allows you to bind a single callback to multiple events. This allows you to reduce your code and aims to make working with multiple devices easier.

Source

Example

local actions = require(script.Parent:WaitForChild'Actions')

local reset = actions:CreateAction("Reset", function ()
	local player = game:GetService'Players'.LocalPlayer
	local character = player and player.Character
	local humanoid = character and character:FindFirstChildOfClass'Humanoid'
	if humanoid then
		humanoid.Health = 0
	end
end)

local active = reset.Active
local toggle = actions:CreateAction("SetActive", function ()
	active = not active
	reset:SetActive(active)
end)

local userInputService = game:GetService'UserInputService'

reset:BindToInput({
	UserInputState = Enum.UserInputState.Begin,
	UserInputType = Enum.UserInputType.Keyboard,
	KeyCode = Enum.KeyCode.R
})

toggle:Bind(userInputService.InputBegan, function (input)
	return input.UserInputType == Enum.UserInputType.Keyboard
		and input.KeyCode == Enum.KeyCode.T
end)
4 Likes

Small issue with your Unbind method:

local a = game.Changed
local b = game.Changed
print(a==b,rawequal(a,b)) --> true false
local t = {}
t[a] = true
print(t[a],t[b]) --> true nil
1 Like

Internally, when you use Bind(signal,func), he stores some data in a table using tab[signal], which makes Unbind(signal) fail as it uses tab[signal] to get this data.

Well, it’ll work if you use the exact same signal, e.g. if you cached game.Changed somewhere and use the cached one for both binding and unbinding actions to events. Just doing Bind(game.Changed) Unbind(game.Changed) would fail.

1 Like

This is definitely a problem, I am looking at removing the Unbind method and returning a disconnectable object.