Best way to deal with multiple copies of the same script parented to objects

  1. What do you want to achieve? Keep it simple and clear!
    First, my apologies for the title of this post. I am having trouble articulating the best way to word this issue. I have multiple copies/instances of the same part/model and each instance has its own copy of a server script. I’m trying to figure out a way to put all these scripts into one place so that edits don’t have to be copied across all these instances and instances don’t have be recreated every time I decide to edit the script.

  2. What is the issue?
    I’m having trouble figuring out the best solution for this problem.

  3. What solutions have you tried so far?
    I have tried to do a bit of searching on the subject but I’m not coming up with anything. It may be due to my inability to word what exactly the issue is. I have considered using module scripts but I’m not certain that would work in this case, I have only made a few module scripts so I’m not sure if I can achieve the desired results with one. On a smaller scale I handled this with a for loop and put everything into arrays/tables. This next script I’m trying to do it with has a great deal of variables with it as well as helper functions and it seems like there may be a better way I just don’t know what that is at the moment. Hopefully I’m making sense here.

Script Example of solution on a small scale:

local object = game.Workspace.myObjects:GetChildren() --pulled in from a folder holding the objects
for i = 1, #object do
    object[i].touched:Connect(function(part)
        object[i].Transparency = 1
        --etc... 
    end)
    
    local switchColor = coroutine.wrap(function()
        while true do
            wait(.5)
            object[i].BrickColor = BrickColor.Random()
        end
    end)
    switchColor()
end
3 Likes

Visit this link for more information:

Furthermore:

For my gun system the Tag Editor did not work for me, So I simply used a script that parents the single script into all the guns at once, The script then auto corrects itself to function properly for each gun.

4 Likes

I’d use modules for this. I generally parent static objects to a folder and then parent a copy of a module for each one at runtime. Then I can control the load order as well as interact outside of the modules.

Another option would be to create a handler function in one module and call it per object. That way each object shares one module script.

Edit: misunderstood the post. I would suggest the above reply in that case

1 Like

Thanks, I’ll have to look further into the collection service and possibly the plugin you mentioned.

1 Like

Sure thing, If it works you should mark my suggestion as the solution. :slight_smile:

I will do that if I end up using it. At the moment I’m thinking it might be overkill for what I’m trying to do. I still need to learn more about the collection service stuff but, it seems to be more for a dynamic runtime situation where an instance is created and then tagged and code is applied to it at that point, I don’t know, more learning required lol.

Kk, Have fun learning Collection Service, Crazyman32 also made a video on it.

1 Like

I gave an example for how you might do this, using CollectionService, on another thread. Hopefully this will point you in the right direction:

2 Likes

Well I’ve been doing some research into the collection service. It is a nice feature but I don’t think it is really necessary for what I’m trying to accomplish. I may use it later but for now all the instances I’m dealing with are already available, so there is no need to track when they are added or removed from the game and I don’t need a quick way of “collecting” them because I know where they are, in a folder. I think my solution is going to end up being a combination of a module script and a simple for loop to initiate the class object with the given instance. The module/class can take over control afterward. The collection service API-reference page is basically doing the same thing except they also use the collection service which in my case isn’t required. All of your posts were helpful but I’ll give the solution to @LeoBlackbane since the collection service example lead me to my solution and because I’ll end up using collection service for something else down the road and since he was the first to mention it.

1 Like

Update: I implemented this solution and it is working perfectly. Thanks again everyone for your help and suggestions.

I created a module script to control the models and simply used a for loop to gather each model and initialize them with a controller instance. The module itself works much like the one found in the Collection Service API reference. Now I don’t have to worry about editing all those extra copies of the same script for each model(woot). I can also see how I may end up using the collection service later for this if I decide to allow new models to be generated or removed from the game.

Script Example:

--Get the Module
local myModule = require(game:GetService("ServerScriptService").Modules.MyModule) 

-- Create the Model Table/Array
local myModels = {}

--Get the models
local child = game.Workspace.ModelFolder:GetChildren()
for i=1, #child do
	if(child[i].Name == "myModel")then
		
		--Initialize a model with a controller
		myModels[i] = myModule.New(child[i])
	end
end