WaitForAttribute?

Is there a way I can make a thing similar to waitforchild but with attributes?

You can probably use a repeat loop. I recommend making a function so you can call it whenever you want without re-writing the whole code. I also added a warn in case it yields for too long.

local function WaitForAttribute(Object, AttributeName, MaxTime)
   MaxTime = MaxTime or 10;
   local Clock = os.clock();
   repeat
      if os.clock() - Clock >= MaxTime then
         warn("Possible infinite yield for "..AttributeName.." on "..Object.Name..".");
         break;
      end;
      game:GetService("RunService").Heartbeat:Wait(); -- You can switch to RenderStepped if you prefer but it shouldn't change that much
   until Object:GetAttribute(AttributeName) ~= nil;
   return Object:GetAttribute(AttributeName);
end;

local Test = WaitForAttribute(Character, "TestAttribute"); -- will yield until "TestAttribute" on Character won't be nil
6 Likes

Mine has some similarities with @DEVLocalPlayer solution, although it’s only checking the attribute when it changes:

function WaitForAttribute(object, name, t)
	local start = os.clock()
	local attribute = object:GetAttribute(name)
	if attribute ~= nil then return attribute end 
	local connection
	connection = object:GetAttributeChangedSignal(name):Connect(function()
		attribute = object:GetAttribute(name)
		if attribute ~= nil then
			connection:Disconnect()
		end
	end)
	repeat task.wait() until os.clock()-start >= (t or 5) or attribute ~= nil 
	if attribute == nil then 
		warn("Possible infinite yield for "..name.." on "..object.Name..".")
	end
	return attribute 
end

--if time is not provided, it will wait for 5 seconds by default
local value = WaitForAttribute(workspace.Baseplate, "test")
print("done!", value)
1 Like

If you only need to wait for the attribute to change then you can use the GetAttributeChanged signal

local Attribute = Instance:GetAttributeChangedSignal("Attribute game")
2 Likes