Is there a way to edit scripts with the same code at the same time?

(I don’t know if this should belong in Dev Discussion, correct me and I’ll change it) Title explains it. So imagine if you make a bunch of scripts with the same line of code, only to find out they have missing loops in it. You then have to fix a bunch of scripts with the same code, which is frustrating and time-consuming. Is there a way to edit scripts with the same code at the same time?

2 Likes

I am not sure if there is a plugin for that, but one solution to prevent this from happening in the first place would be to use ModuleScripts to call that function in the module from each script, or if you want 1 script that does the same job within every part like a kill brick in an obby, you could use this service:

2 Likes

That’s a good idea, but what if it’s the case for new programmers, who don’t know about CollectionService yet, nor modules.

Ideally you would use the modular programming paradigm or similar to approach this. One-script frameworks are usually very manageable as opposed to multiple ones.

1 Like

Ah, okay. I’m actually facing the problem of multiple scripts with the same name right now, hence why I came to write about this post, but thanks for the tip.

Instead of using multiple scripts, merge them together into one where it is a “factory” for the functionality per such instance. The script would be using functional programming paradigm. Using this method, you need to write a function that applies to all these instances. If the instances are different by some aspects, include properties or some alternatives(or maybe use the function as its own environment per instance).

1 Like

Ah okay, thanks for those tips!

You should use module scripts for that

It’s a pretty good choice, since I don’t know how to trigger prompts in CollectionService yet…

Unsure why the response since this is what I suggested?

He probably didn’t see your reply, but thanks everyone for their tips.

1 Like

Is there a way to edit scripts with the same code at the same time?

Not directly. If you have the same script in a hundred places, you have to edit or replace all 100 scripts.

I’ll add a little bit more to the thread that’s missing.
Here’s an example kill brick script that would work when put in a Script inside a Part:

function onTouched(hit)
	local humanoid = hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid")
	if not humanoid then return end
	humanoid:TakeDamage(humanoid.Health) -- TakeDamage does not kill characters with forcefields
end

script.Parent.Touched:Connect(onTouched)

If you had this script in 100 parts and wanted to make them kill characters with forcefields, then it’d be so annoying to delete and replace each of them with a new version.

The wiki has a tutorial on using CollectionService that also uses a kill brick as an example:

If you removed all the scripts from the kill parts, tagged them (with a tag editor plugin) and used the script from the wiki, you could edit one script and have all the parts change in behavior.

In the days before CollectionService, you’d do it nearly the same way, except instead of getting all parts tagged with KillBrick, the script would look everywhere for parts that were named or colored in a certain way

And here’s how you would do it with a ModuleScript.
Put this code in a Script in each of the 100 kill parts:

local onTouched = require(game:GetService("ServerScriptStorage").KillPartModule)

script.Parent.Touched:Connect(onTouched)

Put this code in a ModuleScript named KillPartModule in ServerScriptStorage:

return function(hit)
	local humanoid = hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid")
	if not humanoid then return end
	humanoid:TakeDamage(humanoid.Health) -- do not kill characters with forcefields
end

You still have 100 Scripts and have to tear them out if you want to change the location or name of the module. It’s not a good demonstration of ModuleScripts, but it works and lets you change the touch function from one script.

1 Like