#best practice to set up tools

1 messages · Page 1 of 1 (latest)

worldly swallow
#

what would be the best possible way to set up tools on starterpack and backpack. I know the simplest and easiest way of doing it is by just putting the script under the tool, but all my tools are set through a module to loop through to see what events are required. by that, I mean cloning an exact script to be put under the tool to check and run the desired tool events, which in my opinion is just a waste of code memory. I attempted to do it in a module, not under a tool but it only runs once. Any ways to make it run whenever let's say its in the backpack or starter pack whenever the events are fired? Or tell me your opinion on this.

bright obsidian
#

You could just have a .new() function in your module for per object usage so that it's fine if it only runs once

worldly swallow
#

i did something like that

#

but for some reason

#

it doesnt run after one activated, or equipped, etc

bright obsidian
#

personally I usually have a metadata module defining tool types and giving object properties for the item class

#

so like you could have a gun item type class and you could have multiple items of type gun in the metadata

#

with different ammo and damage stats

worldly swallow
bright obsidian
#

Yea mine's a dictionary too I just call it metadata out of habit lol

worldly swallow
#

my point is

#

henever i try to run the tool events in a module

#

it only runs once

#

hen its in the players backpack

#

how would if fix this?

bright obsidian
#

hence the .new()

#

assume this is the way you define it

worldly swallow
#

ill sho u an example

#
function Item.Setup(player, itemName, itemType, removing)
    local self = setmetatable({}, Item)
    
    self.player = player
    self.item = itemName
    self.itemType = itemType
    self.itemDictionary = require(itemDictionaries[itemType])[itemName]
    
    self.findItemFolder = gameItems:FindFirstChild(itemType):FindFirstChild(itemName)
    
    assert(self.findItemFolder, "Attempted to find item data file using: "..tostring(itemName))
    
    local categorySingular = string.gsub(itemType, "s", "")
    local setupItem = require(script:FindFirstChild(categorySingular)).Setup
    
    assert(setupItem, "Attempted to require item data file using: "..tostring(categorySingular))
    local itemSettedUp
    
    if removing then
        itemSettedUp = setupItem(player, itemName)
    else
        itemSettedUp = setupItem(player, itemName, self)
    end
    
    self.tool = itemSettedUp
    return itemSettedUp
end
#

thats my setup for an item

#

any item

#

and then gear

#
function Gear.Setup(player, gearName, itemTable)
    local self = setmetatable({}, Gear)
    
    local playerCache = player:FindFirstChild("PlayerCache")
    if not playerCache then
        playerCache = Instance.new("Folder")
        playerCache.Name = "PlayerCache"
        playerCache.Parent = player
    end
    
    if not itemTable then
        local toolBackpack = player.Backpack:FindFirstChild(gearName)
        if toolBackpack then
            toolBackpack:Destroy()
        end
        
        local starterTool = player.StarterGear:FindFirstChild(gearName)
        if starterTool then
            starterTool.Parent = playerCache
        end
        
        return "Successfully removed gear"
    end
    
    self.player = player
    self.name = gearName
    self.itemTable = itemTable
    
    -- check if tool exists
    local itemTool = playerCache:FindFirstChild(gearName)
    
    if not itemTool then
        itemTool = itemTable.findItemFolder:FindFirstChildWhichIsA("Tool")
        
        if not itemTool then
            itemTool = self:Create()
        end
        
        itemTool = itemTool:Clone()
        itemTool.Parent = playerCache
        
    end
    
    local starterTool = player.StarterGear:FindFirstChild(itemTool.Name)
    if not starterTool then
        starterTool = itemTool
        starterTool.Parent = player.StarterGear
        
    end
    
    local toolParent = player.Backpack:FindFirstChild(starterTool.Name)
    if not toolParent then
        toolParent = starterTool:Clone()
        toolParent.Parent = player.Backpack
        self:Set(toolParent)
    end
    
    return itemTool
end
#

hich essential fires the desired tool events

bright obsidian
#

so what is the calling order here

worldly swallow
#
self:Set(toolParent)
#

hich does this

#
function Gear:Set(tool)
    local dictionary = self.itemTable.itemDictionary
    local eventsRequired = dictionary.Setup.EventsRequired
    if eventsRequired then
        for key, value in pairs(eventsRequired) do
            if value == true then
                self[key](self, tool)
            end
        end
    end
    
end
bright obsidian
#

does gear not use item as a base class?

#

hmm

worldly swallow
#

they key is the event name

#

like for example

#

the table is like this

["EventsRequired"] = {
                ["Activated"] = true,
                ["Deactivated"] = false,
                ["Equipped"] = true,
                ["Unequipped"] = false,
            };
bright obsidian
#

show me the event processors

worldly swallow
#
function Gear:Equipped(tool)
    print("Gear Equipped Debug: "..tostring(tool.Name))
    local itemFunctions = self.itemTable
    local dictionary = self.itemTable.itemDictionary
    local object = tool:FindFirstChild("Handle") or tool:FindFirstChild("BodyAttach")
    
    tool.Equipped:Connect(function()
        print("equpped")
        local equipSound = dictionary.Skins.Default.Sounds["Equipped"]-- temporary 
        if equipSound then
            local sound = object:FindFirstChild("Equipped")
            if not sound then
                sound = Instance.new("Sound")
                sound.Name = "Equipped"
                sound.Parent = object
                sound.SoundId = "rbxassetid://"..equipSound -- temporary
            end
            sound:Play()
        end
    end)
end
#

is for equipped

bright obsidian
#

like how is self[key](self, tool) connecting to the relevant event

worldly swallow
#

imagine this

worldly swallow
#

it ill find the key

#

if its true it ill fire

bright obsidian
#

Yea I got that part but why are you indexing self for the key, does self have an "Activated" function? If it does and your intention is to fire on that event, calling them all when Set is called won't yield the expected behavior, it just fires it off once

worldly swallow
bright obsidian
#

you gotta use :Connect() on the tool object since it is what has the relevant events

bright obsidian
#

So does the print come through to the output? Also Imo you should use a maid to control it on a per object basis so you can abandon relevant connections when unequipped occurs

#

or do it when the object leaves the inventory and the character model (dropped or destroyed tool)

#

If the print in your Equipped function doesn't come through put a print in the Set function

worldly swallow
bright obsidian
#

And if that doesn't come through then you need to go up the call stack and look at where you call Set

worldly swallow
#

so does the cahced tools

bright obsidian
#

So the problem is that it can't call Equipped for more than one tool at a time or something? What is the issue specifically

#

So it's not possible for items to leave the player's inventory?

worldly swallow
#

hat if the player needs it again

bright obsidian
#

I'm just trying to understand your implementation

worldly swallow
#

so thats hen the cached tools come in

#

hich i parent to a player

#

one sec

#

let me sho u hat i mean

#

like that

#

hen the plyaer doesnt need it

#

i put it under a folder

#

for incase the player needs again

#

also i ant to ask something

#

if a player leaves

#

ith the cached tools

#

ill the tool connections automaticly disconnect?

bright obsidian
#

I see. Might not be super reliable if BreakJoints ends up carrying into the tool it could require you to reconstitute the joints. Maybe that doesn't happen idk lol

#

So all client side code becomes irrelevant when the player leaves

worldly swallow
#

hat if its in the server

bright obsidian
#

Server code, if it descends the object, will terminate execution and cleanup when the object above it is destroyed

worldly swallow
#

automaticly right?

bright obsidian
#

If it's handled in a module, then it really depends on whether an instance of the module is referenced anywhere in active memory

#

So like for example if you created a gun handler and the player left, it would clean up the gun handler so long as there is no scopes that have a variablized reference to the gun handler instance that was previously instantiated

worldly swallow
#

ok but hat if its in a module and i set up the connections in the module for the player

#

ho ould that go in regards to lets say if the player left

#

and the tools became unexistiant

bright obsidian
#

You're gonna have to work on an instance basis anyway otherwise the serverside code will conflict when multiple players use the same tool type

#

When an object destroys, all connections are cleaned up

#

So if you in your toolcache say it destroys it all when the player removes

#

the connections should be cleaned up

worldly swallow
#

ok

bright obsidian
#

but if I was you, I would deliberately destroy the toolcache on player removal

#

just to ensure it doesn't hang out in memory or anything

worldly swallow
worldly swallow
worldly swallow
#

so any ideas anyone?

tulip mulch
#

then connect a function

#

or Humanoid.Changed

#

if Humanoid.Health == 0 then

worldly swallow
#

not as in that lol

#

shouldve said it more clearly

#

one sec

worldly swallow
# tulip mulch Humanoid.Died

my point is i made a module that sets the tools up hen the player equips them hoever hen i reset the tools are no longer orking as the backpack tools are re-replicated. You think i should just loop through the backpack after the player dies reconnecting the tool connections?

worldly swallow
tulip mulch
worldly swallow
#

yes

tulip mulch
#

and you want to reset everything when the player dies?

worldly swallow
#

no

#

hen the player dies

#

the tools no longer ork since the connection of a module is somehere else

tulip mulch
#

:Disconnect() them

worldly swallow
#

also mb my www key isn't working at its finest

tulip mulch
#

same lol

worldly swallow
tulip mulch
#

yes

#

use Humanoid.Died to detect when he dies

worldly swallow
#

but what if i want the tools to connect it again

#

ould i just loop through?

#

again

tulip mulch
worldly swallow
#

u didnt get it lol

#

my point is

#

hen a player dies

#

the backpack tools also dissapear

#

and hen the player respans

#

the starterpack tools clone

#

and reappear

#

ould it be a good practice for me to loop through the backpack to connect the tool functions?

tulip mulch