Placing a Large Number of Duplicate Parts and Scripts - Best Proposal

Hello All,

I’m a new member, first post, learning the basics. Excited!

The scenario and question I present is: I have a game map with 100+ coins placed across it. Each coin is scripted to be “picked-up” when touched by a player. By “picked up” I mean the a coin becomes transparent, with a debounce to not be picked up again for 10 seconds, and a value of 1 is added to the player’s Leaderstats. My question for the Developer Community is: What is the cleanest, least software/hardware taxing way to set up this scenario in Roblox Studio?

These are possibilities I can think of, based on my emerging knowledge:

Option #1- Place all the coins on the game map.
Under Workspace, create a folder to hold all 100+ coin parts.
Insert a “pick-up” Script into each coin.
Thoughts: This seems to be a poor choice because each coin has its own Script, so there are 100+ scripts running, creating excessive work on the server. Plus if the “pick-up” script needs to be changed, each of the 100+ Scripts have to be individually modified.

Option #2- Place 100+ small parts around the game map to serve as spawn locations for the coins. The spawn parts are transparent and CanCollide is false.
Under Workspace, create a folder to hold the 100+ spawn parts.
Under ReplicatedStorage have a coin Model containing both the coin and “pick-up” Script.
Under ServerScriptService create a Script. In the Script use a “for loop” to go through the folder with the spawn parts, cloning the coin Model into the position of each spawn part.
Thoughts: Better than Option #1 because there is only 1 script to change. Bad because there are still 100+ scripts running and the location parts are now extra items that must be loaded into the world.

Option #3- This is where I’m unsure, but I’m thinking…
Place all the 100+ coins directly on the map
Under Workspace create a folder to hold all 100+ coin parts. No script is attached to any of the coins.
In ServerScriptService create a Script with a “for loop” to go through the folder with the coins, trying to detect if any of the coins are touched, and if so then carry out the commands associated with picking up the coin. All that in one Script.
Thoughts: If this could be done, there would only be one Script running for all coins and only one coin Script to ever modify. This option also has fewer parts in the world, compared to Option #2.

Final thoughts: I am currently using Option #2 in my game and don’t like the idea of so many scripts running. I’ve seen some people mention tagging and using CollectionService in other posts, but that idea wasn’t agreed upon in the comments as being ideal. Please let me know what you have found to be the cleanest way to implement a high number of the same parts/scripts in a game, with minimal server and performance impact. Thanks!

2 Likes

I would use one script to control all the coins with a for loop because 100+ scripts running simultaneously adds up fast.

Once script would be the go to for this, because it conserves memory that can optimize games for low end players with not a huge amount of RAM, although talking about processing it’s only so much faster.

False, collection service doesn’t make things slower, in fact, it can make things faster and more understandable. Let’s say you have the coins tagged as “Coins”, instead of having a folder dedicated to coins, you can just tag all the coins and loop through them. Collection Service is really useful, and you should get into the habit of using it.

This might be helpful:

for _, coin in pairs(coins) do
 coin.Touched:Connect(function(hit)
  if not table.find(debounce, coin) then
   spawn(function() -- runs a separate thread
   table.insert(debounce, coin)
   -- deal with the coin giving
    wait(10)
    table.remove(debounce, table.find(debounce, coin))
   end)
  end
 end
end

(not this is a rough draft; it definitely needs some improving. :slight_smile: )