How to get an attributes name

Hello! I am trying to get an attributes name in my roblox game. When I do print(Atributes name goes here) it prints the value. How can I get it to print the name?

Please write a detailed question.

Your_Object:GetAttribute("Your_Attribute")
local attribute = object_name:GetAttribute("attribute_name")
print(attribute)

Here is a method that could work:

function getAttributeName(object, value)
    local attributes = object:GetAttributes()

    for name, val in pairs(attributes) do
        if val == value then
            return name
        end
    end
end

Though unfortunately this will not work correctly if 2 attributes have the same value.

2 Likes

Well. So you know how theres the value of an attribute? Example: I have an attribute named Owner then the value is a players name. I want to get the Name which is Owner. Not the value which is the players name

Thank you! I will try this later.

You would have to iterate through all current attributes using a for loop.
The for loop would give you the attribute name and value for that given instance.

An example of how it would work:

local Player = ...
local TargetName = "Owner"

for AttributeName, Value in ipairs(Player:GetAttributes()) do
	if AttributeName == TargetName then
		-- Your code.
	end
end

A better explanation could be found on the Attributes’ documentation.