Does :FindFirstChild() run each time it is addressed?

Pretty self explanatory, let’s say I set a variable using :FindFirstChild().

Here is my example:

local Bullet = workspace:FindFirstChild(“Bullet”)

If I do this and I address the Bullet, is it using :FindFirstChild() each time? I understand when using RunService using :FindFirstChild() and :WaitForChild() is incredibly inefficient.

Full example:

local Bullet = workspace:FindFirstChild(“Bullet”)

game:GetService(“RunService”):Connect(function()
if Bullet then
– Do something
end
end)

Would doing if Bullet then be considered inefficient? Or is :FindFirstChild() only called once?

In your example :FindFirstChild is only called the once. The result will either be nil or the bullet it found.

Yes, but my question is essentially, if I call “Bullet” in a function, would that be calling :FindFirstChild() each time “Bullet” is mentioned? Or is it only called once, when it is initialized?

No it won’t look for the first child every time Bullet is referenced. Once bullet has been set it will just use that variable value.

Okay epic! Thanks a lot this really helps

1 Like