Question concerning effeciency

i saw a post asking about a good way to make a projectile that hit multiple enemies from september 2018 ish. my solution was this

block.Touched:Connect(function(hit)
    local hitmodel = hit.Parent
    if hitmodel:FindFirstChild('Humanoid') then
        local blockhashit = hitmodel:FindFirstChild('blockhashit')
        if blockhashit == nil then
              blockhashit = Instance.new('StringValue')
              blockhashit.Name = 'blockhashit'
              debris:AddItem(blockhashit,1)
        ~~effect here
        end
    end
end)

i’m wondering if this code is ineffecient or if there’s something bad about it, thanks.

1 Like

The code looks completely fine to me. If you want you can make a temporary table of all the players who have been hit and compare to the new hit person to the table to make sure the same person is not getting hit twice to prevent the code from running more then once.

But generally, this fine.

You shouldn’t create and destroy instances to keep track of cooldowns. You should do as @Sentross suggested in that regard. Just make sure you use a hash table, so that your search operations are done in constant time instead of linear.

This code isn’t inefficient, but I would recommend using CollectionService over creating instances.

Relevant Pages
AddTag to add the tag, which would be used instead creating an instance.
HasTag to check if the model has the tag, instead of checking if the model has a child named ‘blockhashit’.

2 Likes