How to correctly organize my scripts?

I was making a GUI that would let me teleport a certain player to a certain game or all players to a certain game (all of that was done with TextButtons and TextBoxes by the way), the thing is… After a while, I started wondering if it’s better to make all the script in just one “Main Script” or making one script for each button and all of that :man_shrugging:

It is easier later on to have just a few scripts and to put them in the ScreenGui.

If you start adding a bunch of scripts and burying them in the menus then you will have a hard time finding all your scripts. You might wind up with scripts “you forgot about” later.

2 Likes

If you want to apply the same code to a lot of similar models or objects, you could use CollectionService and only have one “Main Script” to update for edits or bugfixes.

This is useful for a large amount of the same object that already exists in studio, so you can edit the code of every object for updates/bugfixes without manually replacing the script in every instance of that object.

Sample Code:

local CollectionService = game:GetService("CollectionService")
CollectionService:AddTag(doorModel, "Door")
local Doors = CollectionService:GetTagged("Door")

-- Loop through items and apply the same code
for _, door in Doors do
     door.ProximityPrompt.Triggered:Connect(function()
          door.Open = true
          -- whatever else you need to do
     end)
end

!!! Keep in mind this is mainly useful for objects that are 100% server-sided or all on the same client. GUIs are always on the client of the player who can see them.

1 Like

I’ll try it out today, thanks!