Which one would be the best?

I know the title is kind-of random to be honest.

But in my game there will be stoplights. And each of them will be scripted.

What if I have 100(s), 1000(s), or even 100000(s), or EVEN 1000000(s) of stoplights. I have two options… either have one script per traffic-light. Or one modulescript that manages all of them. But which one will be better? modulescript, or one script per traffic-light?

My game is going to be large!

1 Like

I’d go with a module script, running a script on each of them (depending on how many there are) would cause unneeded and avoidable lag.

1 Like

Will courotines be necessary?

30 Chararara

I’d definitely recommend one module, CollectionService could be useful for this.

3 Likes

To add on to @MrMoonjelly’s post, I would use CollectionService to round up all the traffic lights. A one line script like this in each traffic light instance:

game:GetService("CollectionService"):AddTag(script.Parent,"TrafficLight")

Would be enough for a script in ServerScriptService to get and function on using CollectionService:GetTagged:

local CollectionService = game:GetService("CollectionService")

local function TrafficLightFunction(trafficLight)
 -- EVERYTHING the traffic light script needs
 -- it might be neater just to make this a module script, require it, and call that the function.
 -- e.g. local TrafficLightFunction = require(script.TrafficLightFunction)
end

CollectionService:GetInstanceAddedSignal("TrafficLight"):Connect(TrafficLightFunction)

for _,trafficLight in ipairs(CollectionService:GetTagged("TrafficLight")) do
	coroutine.wrap(TrafficLightFunction)(trafficLight)
end
4 Likes

Can’t you add tags in studio or do you really need a script to do it?

2 Likes

You could use this plugin: Tag Editor Plugin

3 Likes

I never knew that you could add tags in Studio! Thank you so much for revealing this!!
You made my job on one of the games I’m developing ten billion times easier!

Seriously, thanks.

cc @3rdhoan123, I just figured out how to use the plugin, definitely on my plugin toolbelt now

2 Likes

Neither. Control them all from a single script. Using multiple scripts isn’t scalable for your system, performant or representative of good practice.

If you need to make a single change, then you need to now edit those several hundred other ones to match. Don’t use LinkedSources either, because that’d require putting a script in every spotlight which is easy to get lost.

Using a ModuleScript won’t solve your problem if you end up using several script instances either. Remember that instances incur a memory cost to be used.

One script or ModuleScript, many lights. One edit, all fulfilled. I’d recommend a code structure similar to @goldenstein64’s.

2 Likes