How would i make a function run for many instances in a game?

Sorry if the title seems confusing but im a little lost how can i make one function responsible for many instances

example: i have a code which makes a bush sway away from the player when the player touches it and in the workspace i have like hundreds of these bushes. Putting a code in each bush instance is very inefficient and i would never consider this. Is there a good way to do this?

!! it doesnt have the be a bush it could be anything

I considered making tags for each bush and looping through the workspace but idk now.


  1. Run on the client
  2. Render only the bushes that are close to the player

It doesnt have to be the player what if i needed a code if anything touches the bush?

A Remote, fire a remote if you are needing a event to happen when you touch a bush. example, when a player touches a bush, there is a 50/50 chance a spider will bite their foot. Simple detect touching on the client then do the random part then if it is the correct 50, send the remote.

1 Like

Use for loops. With for loops, you could control every bush with a single script.

Yeah i thought of doing that at first but i found @Ihaveash0rtname’s solution better


you can use the for loop and touched event combined, so it looks like this:

for index, bush in pairs(bushFolder) do

this way “bush” is defined as any of the bushes in the bush folder, which you get by defining the location of the folder and then using :GetChildren to get create the table for the loop. If you don’t want to make a folder for the bushes, you can create the table by looping through wherever there bushes are parented to with a for loop, and then using string.match to find instances with “bush” in them and adding them to the table, thereby having a table with only bushes.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.