Roblox Guide Help

Pets Module


This module aims to handle pet movements and replication on all clients.

Installation

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

[dependencies] pets-module = "release/pets-module@0.0.2"

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 PetsModule = require(ReplicatedStorage.Packages["pets-module"])
local PetsController = Knit.GetController("PetsController") local PetsService = Knit.GetService("PetsService")

Configuration

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

local Pets = { ["NameFramePath"] = { "Assets", "Frames", "PetName" }, ["ModelsPath"] = { "Assets", "Pets" } ["Dog"] = { ["Name"] = "Dog", ["Power"] = 10, ["Rarity"] = "Common", ["Movement"] = "Walk" } } for Pet, Info in Pets do if Info.Name ~= nil and not string.find(Info.Name, "Gold") then local GoldName = "Gold " .. Pet Pets[GoldName] = { ["Name"] = GoldName, ["Power"] = Info.Power * 2, ["Rarity"] = Info.Rarity, ["Movement"] = Info.Movement, } end end return Pets

Explanation:

  • NameFramePath: The path to the BillboardGui that will be displayed above the pets, displaying the name and the rarity. Starts in the ReplicatedStorage.

  • ModelsPath: The path to the folder where the models of the pets are stored. Starts in the ReplicatedStorage.

  • Dog: The internal name of the pet. Can be anything, as long as it is unique.

    • Name: The name of the pet that will be displayed above it.

    • Power: The multiplier of the pet.

    • Rarity: The rarity of the pet. Can be one of those: Common, Uncommon, Rare, Epic, Legendary, Mythical or Exclusive.

    • Movement: The movement type of the pet. Can be Walk or Fly.

Usage

Client Side

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

All those events, except OnPlayerPetsUpdated, are sent to both the client and the server. Server side, the player is sent as the first parameter.

List of events:

  • OnPetAdded: Called when a player owns a new pet. Sends the new pet as a parameter.

  • OnPetRemoved: Called when a pet is removed from a player. Sends the deleted pet as a parameter.

  • OnMaxOwnedUpdated: Called when the limit of owned pets from a player is updated. Sends the new limit as a parameter.

  • OnMaxEquippedUpdated: Called when the limit of equipped pets from a player is updated. Sends the new limit as a parameter.

  • OnPetsUpdated: Called when the player's pet list is updated (pet equipped / unequipped / deleted). Sends the list of all the pets owned by the player, and the list of the pets he has equipped.

  • OnPlayerPetsUpdated: Same thing as OnPetsUpdated, but when another player updates his pets. Sends the player before the two other parameters.

Usage

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


PetsModule:RegisterEvents("OnPetAdded", function(pets) print("A pet has been added to the player. Updated pets list: ", pets) end)

    This method allows a player to toggle the visibility of all pets in the game. When set to false, no pets will be visible.

    Usage

    PetsModule:SetPetsVisible(visible)

    Explanation

    • visible: Whether pets are visible or not.


    PetsModule:SetPetsVisible(false)

      This method allows you to respawn a player's pets. It is mainly used when the player is teleported on a long distance, to avoid the travel time of the pets.

      Usage

      PetsModule:RespawnPets(pets)

      Explanation

      • pets: The list of the player's equipped pets.


      local success, pets = PetsModule:GetEquippedPets():await() if success then PetsModule:RespawnPets(pets) end

        Here are two methods to get the pets of the player.

        Get all pets

        This method returns the list of all the pets a player has in his inventory.

        PetsModule:GetPets()

        Explanation

        • player: The player for whom to modify the limit.


        local Pets = PetsModule:GetPets() print("Player's pets: ", Pets)

          Get equipped pets

          This method returns the list of all the pets a player has equipped.

          PetsModule:GetEquippedPets()

          Explanation

          • player: The player for whom to modify the limit.


          local Pets = PetsModule:GetEquippedPets() print("Player's equipped pets: ", Pets)

            Here are two methods to get the limits of pets for the player

            Get owned limit

            This method returns the limit of pets a player can own

            PetsModule:GetMaxOwned()

            local limit = PetsModule:GetMaxOwned() print("Player can own " .. limit .. " pets.")

              Get equipped limit

              This method returns the limit of pets a player can equip

              PetsModule:GetMaxEquipped()

              local limit = PetsModule:GetMaxEquipped() print("Player can equip " .. limit .. " pets.")

                Server Side

                This method allows you to create a pet outside the default configuration specified above.

                Usage

                PetsModule:CreatePet(name, config)

                Explanation

                • name: The internal name of the pet.

                • config: The configuration of the pet. Same as for the configuration.


                local Pet = PetsModule:CreatePet("Magic Dog", { ["Name"] = "Magic Dog", ["Power"] = 1000 ["Rarity"] = "Epic", ["Movement"] = "Fly" }) print("Pet " .. Pet.Name .. " has been created.")

                  This method allows you to add a pet to a player. It returns a UUID specific to this pet for this player. Without this UUID, you can't equip it, unequip it or even remove it. By default, a player can't own more than 70 pets.

                  Usage

                  PetsModule:AddPet(player, pet)

                  Explanation

                  • player: The player to whom to add the pet.

                  • pet: The internal name of the pet, specified in the configuration.


                  local UUID = PetsModule:AddPet(player, "Dog") print("The pet has been added to the player. Its UUID is: " .. UUID)

                    This method allows a player to equip a pet from his inventory. By default, the player can't equip more than 4 pets at a time.

                    Usage

                    PetsModule:EquipPet(player, uuid)

                    Explanation

                    • player: The player equipping the pet.

                    • uuid: The UUID of the pet to equip.


                    local UUID = PetsModule:AddPet(player, "Dog") PetsModule:EquipPet(player, UUID) print("The pet with UUID " .. UUID .. " has been equipped.")

                      This method allows a player to un-equip a pet from his inventory.

                      Usage

                      PetsModule:UnequipPet(player, uuid)

                      Explanation

                      • player: The player un-equipping the pet.

                      • uuid: The UUID of the pet to unequip.


                      local UUID = PetsModule:AddPet(player, "Dog") PetsModule:EquipPet(player, UUID) PetsModule:UnequipPet(player, UUID) print("The pet with UUID " .. UUID .. " has been un-equipped.")

                        This method allows a player to equip the best pets he has. Pets are ordered by their power, and not their rarity.

                        Usage

                        PetsModule:EquipBestPets(player)

                        Explanation

                        • player: The player equipping the pets.


                        PetsModule:EquipBestPets(player)

                          This method removes a pet from a player's inventory. This cannot be undone, so be careful using it !

                          Usage

                          PetsModule:DeletePet(player, uuid)

                          Explanation

                          • player: The player to whom the pet is to be removed.

                          • uuid: The UUID of the pet to remove.


                          local UUID = PetsModule:AddPet(player, "Dog") PetsModule:DeletePet(player, UUID) print("The pet with UUID " .. UUID .. " has been removed from the player's inventory.")

                            Here are two methods to update the pets limit of a player.

                            Update owned pets limit

                            By default, this limit is set to 70.

                            PetsModule:SetMaxOwned(player, amount)

                            Explanation

                            • player: The player for whom to modify the limit.

                            • amount: The new limit.


                            PetsModule:SetMaxOwned(player, 75)

                              Update equipped pets limit

                              By default, this limit is set to 4.

                              PetsModule:SetMaxEquipped(player, amount)

                              Explanation

                              • player: The player for whom to modify the limit.

                              • amount: The new limit.


                              PetsModule:SetMaxEquipped(player, 75)

                                Here are two methods to get the pets of a player.

                                Get all pets

                                This method returns the list of all the pets a player has in his inventory.

                                PetsModule:GetPets(player)

                                Explanation

                                • player: The player for whom to modify the limit.


                                local Pets = PetsModule:GetPets(player) print("Player's pets: ", Pets)

                                  Get equipped pets

                                  This method returns the list of all the pets a player has equipped.

                                  PetsModule:GetEquippedPets(player)

                                  Explanation

                                  • player: The player for whom to modify the limit.


                                  local Pets = PetsModule:GetEquippedPets(player) print("Player's equipped pets: ", Pets)
                                    Last modified: 27 November 2024