How would I check if any children are tagged?

I’m working on an item system for a game similar to Piggy and need a way of locating an instance based on it’s tag.

Unlike Piggy, my game doesn’t use the standard Tool system for the items, instead it parents the items to the player before welding them directly to the players hand. This has worked well, however, it means that when an item is already in the players hand, the game needs to know this and swap them.

My current solution has been to name all the items “Item Part” and use FindFirstChild to locate them, the issue of course being that in a game with about a dozen items, having them all named the same is inconvenient.

if character:FindFirstChild(“ItemPart”) then

Ideally, I want to essentially have all item names be unique, and find them based on their tag.

Is there any viable solution fort this?

2 Likes

I would recommend using CollectionService to tag all of the tools with a “Tool” tag, and then adding a string attribute to each tool called “ItemName” that would have the item’s name as it’s value (eg, “ItemName” having a value of “RedKey”).

You can then reference these tools later without worrying about their explorer hierarchy and their actual name.

1 Like

you can tag the instances: when you create your item instances, you can use the CollectionService to tag them with a specific tag that is unique to each item type. i.e, you might use the item’s name as the tag.

local CollectionService = game:GetService("CollectionService")

-- Create the item
local newItem = Instance.new("Part")
newItem.Name = "ItemPart"

-- Tag the item
local itemName = "unique_item_name" -- Replace with the actual unique name of the item
CollectionService:AddTag(newItem, itemName)

-- Parent and weld the item
newItem.Parent = player.Character
-- Welding code...

then locate the items by tagging them;


local CollectionService = game:GetService("CollectionService")

local itemNameToFind = "unique_item_name" -- Replace with the actual unique name of the item
local itemsWithTag = CollectionService:GetTagged(itemNameToFind)

for _, item in ipairs(itemsWithTag) do
    -- Perform your actions on the found items
    -- For example, you can check if the item is already in the player's hand
    if item.Parent == player.Character then
        -- Swap the items or take appropriate action
    end
end

when you want to locate an item based on its tag, you can use the CollectionService 's GetTagged method. This will return a list of instances with the specified tag. (as Katrist said)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.