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.
#best practice to set up tools
1 messages · Page 1 of 1 (latest)
You could just have a .new() function in your module for per object usage so that it's fine if it only runs once
i did something like that
but for some reason
it doesnt run after one activated, or equipped, etc
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
i technically did something like that ith a dictionary
and yeah
Yea mine's a dictionary too I just call it metadata out of habit lol
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?
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
so what is the calling order here
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
they key is the event name
like for example
the table is like this
["EventsRequired"] = {
["Activated"] = true,
["Deactivated"] = false,
["Equipped"] = true,
["Unequipped"] = false,
};
show me the event processors
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
like how is self[key](self, tool) connecting to the relevant event
imagine this
the key is lets say
Equipped in this table
it ill find the key
if its true it ill fire
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
i ant to connect the events tho
you gotta use :Connect() on the tool object since it is what has the relevant events
is hat i did
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
i mean i set it like this its like cached tools once its unequipped its put under the player with a folder
And if that doesn't come through then you need to go up the call stack and look at where you call Set
if the player leaves
so does the cahced tools
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?
no i dont ant it like that. its like equipping and unequipping tools
hat if the player needs it again
I'm just trying to understand your implementation
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?
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
hat if its in the server
Server code, if it descends the object, will terminate execution and cleanup when the object above it is destroyed
automaticly right?
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
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
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
ok
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
ok
the issue is hen i reset my character. yk ho it re-replicates the tool from starterpack, therefore making it no longer ork
I'm ondering ho exactly should i detect once a player resets their character
so any ideas anyone?
Humanoid.Died
then connect a function
or Humanoid.Changed
if Humanoid.Health == 0 then
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?
also ur opnion on this @tulip mulch
idk if i understood correctly , but you made a custom Tool Module ?
yes
and you want to reset everything when the player dies?
no
hen the player dies
the tools no longer ork since the connection of a module is somehere else
:Disconnect() them
also mb my www key isn't working at its finest
same lol
when the player dies?
but what if i want the tools to connect it again
ould i just loop through?
again
you could just Clone the Tools that you want and give it to the player
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?
loop trough the backpack and destroy the tools