Attach your own data to any object with SetAttribute and GetAttribute, so parts describe themselves.
Sometimes you need to attach your own data to an object: "this part is a coin," "this NPC is friendly," "this door is locked." That's attributes — sticky notes that make objects describe themselves.
Two methods do everything:
part:SetAttribute("IsCoin", true)
part:SetAttribute("Value", 10)
print(part:GetAttribute("IsCoin")) --> true
print(part:GetAttribute("Value")) --> 10
SetAttribute(name, value) sticks data on the object; GetAttribute(name) reads it back. The value can be a number, string, boolean, or more. It survives as long as the object does.
Attributes are readable from anywhere — server or client — and they don't confuse the engine. They're the standard way to make objects self-describing.
To read a value you set earlier, call part:("IsCoin").
The old way to identify a part was checking its name:
if part.Name == "Coin1" then -- breaks with 50 coins
Names can't describe 50 coins individually. Attributes can — and one check handles all of them:
local coin = workspace:WaitForChild("Coin")
if coin:GetAttribute("IsCoin") then
print("It's a coin worth " .. coin:GetAttribute("Value"))
end
Any script that meets a part can look at its attributes and know what to do with it — no hardcoded name list, no magic strings.
Which of these stores 'this part is a coin' so any script can read it?
Attributes really shine when you generate objects in a loop — each one carries its own data:
for i = 1, 5 do
local coin = Instance.new("Part")
coin.Size = Vector3.new(1, 1, 1)
coin.Anchored = true
coin:SetAttribute("IsCoin", true)
coin:SetAttribute("Value", 10 * i)
coin.Parent = workspace
end
Five coins, five different values, one attribute check to handle them all. The coin's worth travels with the coin.
A door part needs to remember it's locked. Write the line that sticks a 'Locked' attribute with the value true onto the door.
-- lock the door
if part.Name == "Coin1" breaks the moment you have 50 coins. GetAttribute("IsCoin") works for all of them.Next: giving the world a voice — sounds.