Where is the difference when i use
if workspace:FindFirstChild("Part") then
and
if workspace.Part then
Where is the difference when i use
if workspace:FindFirstChild("Part") then
and
if workspace.Part then
with that you can use spaces in instances to find
while the other you cant
workspace.Part
is the dot operator, behaves equally to workspace["Part"]
, however, in the second one you can freely use spaces and numbers (it’s a string) (workspace.1Part is not supported in dot syntax but workspace.Part1 is)
If you use the dot operator, you will have these:
→ Faster
→ Quicker and easier to write
However, these are the bad things:
→ If it doesn’t find a member (property, event, child, method, etc) then it will error
→ If you are trying to index an instance, you risk the possibility of there being a property called like that and retrieving the property
Whereas findfirstchild is slower but goes through all the instance’s children until it retrieves the one you want, without erroring (but returning nil if it doesn’t exit), and you don’t need to worry about properties with that name
Dot operator should only be used when you know something is supposed to exist and it doesn’t interfere with other members (events, properties, etc…)
FindFirstChild should be used when you are not certain something exists and can confuse it with a member (as said before).
You can imagine an array and a dictionary as an example. As you may know, arrays only have number indices that go from 1 to increasing in order (must be 1, 2, 3 etc).
Dot operator (or bracket syntax) is similar to indexing to a dictionary / array
local table = {
[1] = "lol",
}
--We can't do:
table.1
--But we can do (as explained before):
table[1]
Lua will index that table and get the value directly without having to do pairs / ipairs / next (for loop). That’s the reason why it’s faster
The difference is, when working with instances, indexing may error us because a such member does not exist (don’t worry, just metatable things that Roblox has supplied instances with)
this mostly a quick find isntance that is known not to have a space in it.
workspace["A Part"]
You can use bracket syntax to reference instances with whitespaces in their names.
i already know that but you can also do with :FindFirstChild("") as asked
That’s the slower alternative of the two.
workspace.Part
> workspace:FindFirstChild("Part")
> workspace:WaitForChild("Part")
I’ve edited my original post so you can have an idea when to use each
okay but he still he didnt ask about workspace[""]
Just because someone doesn’t ask about something doesn’t mean you shouldn’t make them aware of that thing.
Difference is that findfirstchild and waitforchild have nothing to do with the dot operator or bracket syntax. They use a complete different method of retrieving an instance
he asked for something spesific of what the diffrence is
not how the best way of finding a part on workspace is.
Can you now stop getting off topic?
If you couldn’t tell ‘>’ indicates which approach is more efficient, I’m well aware of the differences.
There is no better method, as each method has its purpose depending on what you are trying to achieve
In my own code I never need to use FindFirstChild
or WaitForChild
, both are bad practices.
U said FindFirstChild is slower so:
if player.Character then
if game:FindFirstChild("Part") then -- The Part will be there all the Time
character... -- Doing something with the Character
end
end
Do i also need to use if player.Character then
under FindFirstChild
because u said its slower, so if the Player leaves before FindFirstChild ends would character error?
How would > work
for the low diffrence of delay i would say totally no
a player cant even leave even on purpose to error the code
because the delay is so small
player.Character is a property, not an Instance (a property that stores an instance!!)
It will always be there, however when there is no character, the value will be nil
I mean, if we get creative, you could use this:
function SafeDotOperator(parent, childName)
return pcall(function()
return parent[childName]
end)
end
local inst = SafeDotOperator(game.Workspace, "lol")
if typeof(inst) == "Instance" then --Only bad case is it could be a property storing an instance
print("Retrieved instance successfully: "..inst.Name)
end