Roblox Guide Help

Fight Module


This module can be used to store player's states about fights while they are online. It allows a player to start a fight against what is called here a NPC. A player can only fight one NPC at a time.

NPCs have a name, power, reward, a boss state (to specify whether they are a boss or not) and are linked to an area. All these properties are purely indicative and are not used by the module. They are sent back to the server when a player win a fight.

Installation

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

[dependencies] fight-module = "release/fight-module@0.0.9"

Import

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 FightModule = require(ReplicatedStorage.Packages["fight-module"])
local FightController = Knit.GetController("FightController") local FightService = Knit.GetService("FightService")

Configuration

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

return table.freeze({ ["NPC_1"] = { ["Name"] = "Fist NPC", ["Area"] = "Area1", ["Power"] = 3_000, ["Reward"] = 10_000, ["Boss"] = false } })

Explanation:

  • NPC_1: The name of the npc.

  • Name: The name to be displayed in the menus

  • Area: The zone the NPC belongs into.

  • Power: The power level of the NPC

  • Reward: The reward when the npc is defeated.

  • Boss: Whether the NPC is a both or not.

Usage

Client Side

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

List of events:

  • OnFightStarted: Called when a player starts a fight.

  • OnFightWin: Called when a player wins a fight.

  • OnFightLoose: Called when a player loses a fight.

  • OnNpcCreated: Called when a NPC is created.

  • OnNpcUpdated: Called when a NPC is modified.

Usage

FightModule: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.


FightModule:RegisterEvents("OnFightStarted", function(NPC) print("Player started a fight with NPC " .. NPC.name .. " !") end)

    Server Side

    This method is used to create a NPC object from its configuration. It is automatically called when the server starts, but can be called manually whenever needed.

    Usage

    local NPC = FightModule:CreateNpc(player, npcName, config)

    Explanation

    • player: The player for whom the NPC is configured.

    • npcName: The name of the NPC to be configured, which must be found in the configuration.

    • config: The NPC configuration for the player, as a table.


    local NPC = FightModule:CreateNpc(player, "NPC_2", { ["Name"] = "Second NPC", ["Area"] = "Area2", ["Power"] = 6_000, ["Reward"] = 20_000, ["Boss"] = true, }) print("NPC '" .. NPC.name .. "' has been created.")

      This method allows you to configure a NPC for a player. Most of the time, it has to be used when the player join the game, retrieving all NPCs he has beaten in the past and sending those information to the module.

      Usage

      FightModule:ConfigureNpc(player, npcName, config)

      Explanation

      • player: The player for whom the NPC is configured.

      • npcName: The name of the NPC to be configured, which must be found in the configuration.

      • config: The NPC configuration for the player, as a table.


      FightModule:ConfigureNpc(player, "NPC_1", { beaten = false, fightable = false })

        This method allows you to start a fight for a player with a NPC. When a player is in a fight state, no other fights can be started.

        Usage

        FightModule:StartFight(player, npcName)

        Explanation

        • player: The player who will fight the NPC.

        • npcName: The name of the NPC against which the player will fight, which must be found in the configuration.


        FightModule:StartFight(player, "NPC_1")

          This method allows you to stop a fight in which a player is. When the fight is stopped, the NPC is marked as fightable again.

          Usage

          FightModule:EndFight(player, npcName, won)

          Explanation

          • player: The player who is fighting the NPC.

          • npcName: The name of the NPC against which the player is fighting, which must be found in the configuration.

          • won: Whether the player has won or not.


          FightModule:EndFight(player, "NPC_1", true)

            Important

            Last modified: 25 November 2024