Roblox Guide Help

Action Module

In your wally.toml file, add this to your dependencies:

[dependencies] action-module = "release/action-module@0.0.19"

You have two solutions to import this module, depending if you wish to import the Controller or the Service on your own, or if you let the module give you the right code.

local ActionModule = require(ReplicatedStorage.Packages["action-module"])
local ActionController = Knit.GetController("ActionController") local ActionService = Knit.GetService("ActionService")

A file called ActionModule.config.lua is to be created in the folder ReplicatedStorage/Shared/Configs.

return table.freeze({ ["Fight"] = { ["Key"] = Enum.KeyCode.E, ["Cooldown"] = 1000 * 5, ["DefaultCallback"] = function(player: Player) print("The Fight action has been used.") return true end, }, })

Explanation:

  • Fight: The name used to refer to your action. This name is unique, it cannot be referenced twice.

    • Key: The Key to which your callback function will be bound.

    • Cooldown: The minimum time required between two uses of the action.

    • DefaultCallback: The default callback function for the action. This can be updated later using Action:BindCallback(callback).

Usage

Client Side Functions

This method allows you to listen to an event sent by the ActionService and run a callback function whenever an event is received.

List of events:

  • OnActionPerformed: Called when a player uses an action. Sends the name of the action as a parameter. Called only if the action succeeds.

Usage

ActionModule:RegisterEvents(name, callback)

Explanation

  • name: The name of the event you wish to listen to.

  • callback: The callback function that will be run whenever this event is received.


ActionModule:RegisterEvents("OnActionPerformed", function(action: string) print("Player performed action named " .. action) end)

    Server Side

    This method is called automatically when a player uses the key bound to an action. However, it can be called manually when needed using this function.

    Usage

    ActionModule:PerformAction(player, action)

    Explanation

    • player: The player performing the action.

    • action: The name of the action that the player will perform.


    local res = ActionModule:PerformAction(player, "Fight") if res == nil then print("The action has been successfully performed.") else print("An error occurred while performing the action: " .. res) end

      This method is called automatically when the server starts, binding the actions to their keycodes from the configuration. However, it can be called manually when needed.

      Usage

      local Action = ActionModule:CreateAction(name, cooldown, callback)

      Explanation

      • name: The name of the action.

      • cooldown: The minimum time required between two uses of the action.

      • callback: The default callback function for the action.


      local Action = ActionModule:CreateAction("Punch", 650, function(player: Player) if player.Name ~= "JusteStop" then return false end print("Player " .. player.Name .. " performed Punch action.") return true end) print("The action named " .. Action.name .. " has been created.")
        Last modified: 25 November 2024