I am at the stage of making my code as fast as possible. I used to have :FindFirstChild() twice to check if a hit part is a player or not. But with the information provided by Roblox, :FindFirstChild() is slow than other functions of finding. So what I did instead was use a Collection Service to serialise all the individual parts within the character and future parts added to the character to a tag. When an object that deals damage hits a player, instead of using :FindFirstChild(), I will use :HasTag().
The player will have a table within the game (not related to data stores) that contains various information where the key is their name. So the 2 tags that players will have is first a “Player” tag, and then their name as the tag after.
Would it be better to check if the player has the tag “Player” first before I use :GetTags() in order to save this function from being called every time a part is hit, or should I immediately use :GetTags() whenever a part is hit. Or is the difference so minimal it doesn’t matter.
You don’t need a tag to identify the player, just simply use .Name to fetch the player name.
GetTags() returns all the tags currently in applied to the object, which you would have to use this code:
local CS = game:GetService("ConnectionService")
local Part = script.Parent
local Tags = CS:GetTags(Part)
if table.find(Tags, "Player") then
return
end
But with :HasTag(), this code would be used:
local CS = game:GetService("ConnectionService")
local Part = script.Parent
If CS:HasTag(Part, "Player") then
return
end
You don’t have to set unnecessary variables so this should be faster.
From the developer hub, referring to GetTags(), “This method is useful when you want to do something with multiple tags at once on an object. However, it would be inefficient to use this method to check for the existence of a single tag.”
Since I am looking for a player tag first and then the name of the player after, would it be better to use :GetTags() which returns the table of tags which looks like this:
Tags = {"Player", "playerName"}
It’s not one tag, but its neither many tags also, just two.
Sounds like you’re starting to reach into microoptimisation grounds which I don’t think should be a priority if you’re only just getting into optimising your code. The difference here is fairly negligible: it’s like comparing fetching a table versus finding something in one.
The better question to be asking here is which one more appropriately suits your use case rather than which one is faster. Will one be faster than the other? I don’t know, you can check with a simple tick test. Does it matter? No, it really doesn’t. This won’t be the crux for speed in your code. Or, another better question is if this needs to be optimised in the first place.
If you get too hellbent on trying to make things fast, you’re just going to make your code look ugly. Optimisation is good but microoptimisation really isn’t important and you can file that away for later if you have that kind of time to be wasting over being productive and adding new features.
If you ask me, each has their own use case in code. Maybe you don’t even need either. I’ve read over your use case a couple of times over and I can’t fully grasp what the intention is and what you’re trying to optimise, rather if it even needs optimisation in the first place. Could be that this isn’t the thing that you should be optimising and rather another part of your code.
I see, maybe I am getting too focused into performance due to previous experience (my previous game crashed so often that players left), but looking at the code, I believe it is for more prospective reasons since my current system cannot support my future intentions. But thank you for the insight!