How do i get Attribute name in a ToString()

Hello, So i tried to set a property using an Attribute but it says argument #1 missing here is the code

v.Material = Enum.Material[tostring(v:GetAttribute("SecMaterial"))]

,btw it is in a for i,v in pairs(workspace:GetChildren()) do

tried it without tostring(), but it didn’t work.

You are getting this error because you are not checking if the Attribute exists first.

I did check with an if statement.

I’m only able to reproduce this issue if the Attribute itself does not exist.
Can you provide the if statement line.

You should check the attribute’s presence within the game before you attempt to retrieve it.

if Instance and typeof(Instance) == "Instance" and Instance:IsA("Instance") and Instance:GetAttribute("Attribute") then
	-- Proceed
end


local function hasProperty(object, prop)
			local t = object:GetAttribute(prop)
	end
for i,v in pairs(game.Workspace:GetDescendants()) do
			local s3 = pcall(function() hasProperty(v, "BitUse") end)
			if s3 then
				if v:IsA("Part") or v:IsA("UnionOperation")  then
					v.Material = Enum.Material[tostring(v:GetAttribute("SecMaterial"))]
                                 end
                         end
end 

Remove the pcall(function()
What is happening is that pcall returns Success and Result
Since it is succeeding, the if statement goes through.

Or you can just add s3, s4 = pcall(function() …
and just check if s4 exists

the pcall(function(), checks for the Attribute

Oh well then here you go.

local hasProperty = script.GetAttribute
for _, v in pairs(workspace:GetDescendants()) do
	local material = hasProperty(v, "SecMaterial")
	if material and hasProperty(v, "BitUse") and v:IsA("BasePart")  then
		v.Material = material
	end
end

You may understand the fact that you can use Instance inheriting functions on a variable.

GetAttribute won’t error if the attribute doesn’t exist so pcall returns Success. (Also you don’t return the property in the function)
@04nv solution should work for you.

1 Like

ok, I’ll go straight to the point how do i get an attribute name using GetAttribute() function.

Instance:GetAttributes is the function you’re looking for.

for index, value in pairs(Instance:GetAttributes()) do
    print(index) -- The attribute name.
end

An attribute name with the GetAttribute function?

If you want to get all the attributes on an Instance: Instance:GetAttributes()
If you want to check if an attribute exists: if Instance:GetAttribute(“Attribute”) ~= nil then

You could just shorten it to this, you don’t need pcall since it just returns nil

for _,v in workspace:GetDescendants() do
	if v:IsA("BasePart") and v:GetAttribute("BitUse") and v:GetAttribute("SecMaterial") then
		v.Material = Enum.Material[v:GetAttribute("SecMaterial")]
	end
end
for i,v in pairs(workspace:GetChildren()) do
print(v:GetAttribute("SecMaterial")) -- to print its value
end

you forgot a do at the end of the for i,v

For the line of code you just sent* @04nv solution solves your original problem

for i,v in pairs(workspace:GetChildren()) do
print(v:GetAttribute("SecMaterial")) -- to print its value
end

This code you sent should work perfectly fine.