I want to detect if a player owns a certain UGC item and award them if they do. How can I do the detection part? Is it possible?
Your post title asks about UGC they’re wearing, but the description asks about UGC they have in their inventory. I’m pretty sure you can’t check inventories of players who made their inventory private.
But you can easily check the UGC they’re currently wearing by looping through all the Accessory instances in the character, and comparing the Mesh ID of the Handle (the mesh inside the accessory) to the ID of the UGC you want to find.
(I could be wrong about the specific property holding the ID, I’ve never made something like this)
I think this would work! But here’s my code so far;
for i,v in pairs(player.Character:GetChildren()) do
if v:IsA("Accessory") then
if v:FindFirstChild("Handle"):FindFirstChild("SpecialMesh").MeshId == 17545463154 then
-- award player
end
end
end
the issue is that i get this error;
attempt to index nil with “MeshId”
even though i double checked everything and even checked my character and made sure the directory was correct and such
You want to compare the MeshId of Handle
, there’s no meshes inside of the Handle, lol. Also, the MeshId property is a string with the rbxassetid://00000000000
format
Will edit the format, but this is the directory for the accessory.
Weird, I took a look at my own character before sending this and didn’t find any meshes inside the Handles.
Might it have anything to do with my game being in R6? Or the UGC being created a few minutes ago?
If you want to detect whether the player is wearing the accessory, you can probably use one of the properties of HumanoidDescription. HumanoidDescription has properties for different accessory types. So if you know both the accessory id and the accessory type, you can do the following.
-- returns a success boolean telling whether the check was succesful (whether the player currently has a character) and a result boolean that is only relevant when the check is succesful.
local function doesPlayerHaveThisAccessory(accessoryId: number, accessoryType: Enum.AccessoryType): (boolean, boolean)
local character: Model = player.Character
if character == nil then
return false, false
end
local humanoidDescription: HumanoidDescription = character.Humanoid:GetAppliedDescription()
local hatAccessoryIdsString: string = humanoidDescription[accessoryType.Name .. "Accessory"]
local givenIdAsString: string = tostring(accessoryId)
for _, accessoryIdInDescription in string.split(hatAccessoryIdsString, ",") do
if accessoryIdInDescription == givenIdAsString then
return true, true
end
end
return true, false
end
Or you could use the MeshId approach suggested by @TestyLike3.
If you want to check this without having to know the accessory type beforehand, then you can either look for the id in all of the accessory properties of HumanoidDescription or use MarketplaceService:GetProductInfo(AccessoryID) to find the accessory type of the accessory and then choose the correct HumanoidDescription property based on that. Here’s a post telling how to do this:
There is also API for getting the items that a player owns but that requires the player to give the permission to view their inventory.
@TestyLike3 and @xHug_h, handles of accessories of R6 characters are all Parts with SpecialMeshes. Whether the handle of an accessory of an R15 character is a MeshPart or a Part with a SpecialMesh depends on the workspace property MeshPartHeadsAndAccessories. If it’s Disabled, the handle is a Part with a SpecialMesh. If it’s Default or Enabled, the handle is a MeshPart. Also, the name of the SpecialMesh is not necessarily “SpecialMesh”. So if you want to take that approach, I’d recommend using :FindFirstChildOfClass(“SpecialMesh”).
its because its a meshpart and not a basepart, you would first check the handle classname, then derive meshid from that or check if they have a specialmesh where you can check its filemesh id
This worked, thank you!! I’ll mark this as a solution, though in some instances the solution provided by @TestyLike3 might work aswell.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.