Iām not 100% sure about my ideas, but hereās my two cents. FYI, Iām assuming what youāre looking for is a way to add the same connection to every tool w/ a single local script, alongside managing what happens when the player drops the tool, has it picked up by someone else, or drops it, resets it, and picks it up?
#1 - Your concerns
- If you drop the tool and someone else picks it up, alongside activating the tool, it wonāt trigger for your client since ā.activatedā in this context is local to each client. If someone else activates the tool, only they will get the .activated event for this local script.
- Iām kind of not sure what you need the tags for. Are you checking for a specific tool name? I thought they all worked similarly, so the tool name wouldnāt matter much. Just store attributes in each tool if you want to track stats, right? I donāt have enough information to understand.
#2 - How to do what youāre asking, if I get it correctly?
Tweak your local script to have a table, which will store two things about any new tool added in your backpack: The instance, and the .activated connection.
This is so that we can easily check if this tool is removed from your backpack later, and also to disconnect the .activated event in case the tool is dropped and handed to someone else (no point in leaving your .activated connection in that case? dunno, garbage collection confuses me).
Next, tweak the local script so itās a starterplayerscript, and change ChildAdded event to instead check when a child is added to the playerās backpack. When this happens, perform the checks for this tool to check if it has the tag youāre talking about (not too familiar with collectionservice but it seems cool), and then store the literal instance as a key inside your table, with its reference being the .activated event which will do your generic stuff.
If you want to make the activation events different for different tools, create a function to identify the type of tool you have (using tags assigned with collection service, for example, or just storing a string value inside your tool), and then you can use that to assign a different function to activate inside of the .activated event for your tool depending on the type of tool you identified (such as swords, spears, hammers, axes).
#3 - This is an example I made. Probably way longer than it needs to be???
local player = game.Players.LocalPlayer -- References the player.
-- References the backpack for our ChildAdded/Removed purposes.
local toolConnections = {} -- How we'll store references to tools in our inventory and event connections we setup.
local function setupBackpack(backpack)
backpack.ChildAdded:Connect(function(child) -- Just tweaked your code to check for backpack's children instead of character.
-- Your original code seemed to have the HasTag check to prevent a script from being misidentified.
-- I think :IsA("Tool") is enough to prevent non-tool items from being checked.
-- If you want to check a tool's specific type or something, you can add that by adding tool attributes.
if child:IsA("Tool") and not toolConnections[child] then -- Check the item's type.
toolConnections[child] = {} -- This adds your tool to the toolConnections array as a key w/ {} as its value.
local toolReference = toolConnections[child] -- Using a variable here for readability.
-- We are creating a key inside the table that corresponds to our tool key. This key has an event as its value.
toolReference.ActivatedConnection = child.Activated:Connect(function() -- Connects a function to the tool's Activated event.
-- Your code here for tool activation.
print("Tool activated.")
end)
end
end)
-- Let's add a childRemoved event to disconnect the event when you drop the tool.
-- BTW, .activated event WON'T fire for your client if someone else activates the tool - .activated is a local event.
backpack.ChildRemoved:Connect(function(child) -- Just the opposite of a childAdded event.
if child:IsA("Tool") and child.Parent ~= player.Character then -- Check if you dropped tool out of inventory.
if toolConnections[child] then
local toolReference = toolConnections[child]
-- Disconnect the .activated event. I don't think it's necessary, but better to be safe than sorry, right?
toolReference.ActivatedConnection:Disconnect()
toolConnections[child] = nil -- Remove the reference to this tool in the table.
end
end
end)
player.CharacterAdded:Connect(function() -- Literally the same code as above but for characters dropping tools.
local char = player.Character
char.ChildRemoved:Connect(function(child)
if child:IsA("Tool") and child.Parent ~= player.Backpack then
if toolConnections[child] then
local toolReference = toolConnections[child]
toolReference.ActivatedConnection:Disconnect()
toolConnections[child] = nil
end
end
end)
end)
end
-- Sets up your initial backpack and character connection. This gets automatically disconnected when you respawn.
setupBackpack(player.Backpack)
LMK if you have any questions/concerns. Thereās probably a simpler solution? My longer solution is good for tracking both tools in backpack and character, disconnecting .activated events for when you drop the tool but you donāt reset, and my script gives you a way to track per-tool values like cooldowns and so on/so forth.