Is it considered bad form to duplicate the same script into a lot of objects?

In normal cases i would make a modulescript and call the needed function from that but this script is called very frequently so it has to be well optimized. The thing is that it has this in it: script.Parent:FindFirstChild(“”) and from what i’ve heard FindFirstChild() is pretty taxing performance wise. If i use a modulescript it has to run FindFirstChild() every time it runs but if i duplicate it to every part then FindFirstChild() can run once outside of the function and just use that value from there on.
What’s your opinion on the matter?

To be honest if they’re just simple scripts such as a kill script, it doesn’t make that much of a difference even though most developers would get mad and yell at you for it. People say FindFirstChild() is “unreliable” and slow but I’ve never had that problem before. But, if you’re really looking for prime optimization, just use CollectionService in on main handler script, and tag all the parts you want to be affected by a script.

2 Likes

If you are using the same script for multiple bricks I would recommend using CollectionService. It could help make it more efficient and reduce the number of scripts needed

If i use CollectionService the problem will still persist because the function will still have to call FindFirstChild() every time it runs.

Can you show the scenario which you are using FindFirstChild() for?

Unless you actually know that it’s causing performance problems, then it’s not time to start optimizing it yet. Instead, you should focus on keeping your project maintainable. If you have 100 Instances that all have that script, it will be a PITA to make even a simple change to it because you’ll have to copy that change to all 99 other scripts. It’s also error prone, as you might miss some scripts causing bugs that can be hard to track down. Having a single script that acts on all of the Instances means you only have to change that single script.

It sounds like you have to define each variable every time the code runs, which, in that case, just store the variables in the actual module. Can we see the script you are using?

Sometimes, FindFirstChild isn’t even needed.

The way i do it now is that there is another script that clones the first script from ServerStorage to everywhere it needs to be at the launch of the game, so i still only have to edit one file really. Altought i have to concede that it’s not very elegant.

This is what it looks like, I’ve cut out unnecessary code. If i wasn’t copying the whole script i would have to move the defines at the start inside the function.

local epart = script.Parent

local eroom = epart.Parent

local ernum = eroom.Name

local epnum = epart.Name

local iroom = eroom.Parent.Parent.big:FindFirstChild(ernum)

local ipart = iroom:FindFirstChild(epnum)

function onHit(hitter)
    --destroys ipart based on circumstances
end

epart.Touched:connect(onHit)

You can use a for loop and loop through each part and store all of the variables in a local code block:

local Blocks = -- the blocks
local Part = -- the self part
for i, v in pairs(Blocks) do
    local TwinBlock = something:FindFirstChild("Something")
    workspace:DescendantRemoved:Connect(function(block) -- Or Child Removed
        if TwinBlock == block then Part:Destroy() end
    end)
end
1 Like