How to detect if a group of items have been touched?

I am working on a powerup system for cars in my roblox game. But if there are so many powerups available to collect in the map I can’t have one variable for each powerup (I am going to use the .touched event). How would I detect if a powerup (Out of all the ones available to collect) is touched?

2 Likes

You could put the powerups in a folder in workspace, and basically make a table of getting the folder’s children (the powerups) Then use:

for i = 1, #PowerUpTable do
      PowerUpTable[i].Touched:Connect(YourFunctionHere)
end

Basically a for loop.

Alternative

You can use any kind of loop that loops through a table. Any loop that you prefer really.

3 Likes

Stemming off of what @fredrick254 said; just going in some more depth.

You don’t only need to use a table that stores all of your powerup’s you can also just parent them to folder and use a for loop there.

local Powerups = game:GetService("Workspace"):WaitForChild("PowerupFolder")

local function doPowerup(powerUp)
   print("Touched powerup ".. powerUp.Name)
end 

for _,v in pairs(Powerups:GetChildren()) do
   if v:IsA("BasePart") then
      v.Touched:Connect(doPowerup)
   end
end
8 Likes

But what if the powerup gets touched before it loops through that part?

The loop @fredrick254 and I both posted would be inside of a server script, assuming you don’t add any delay to it; it’ll run right when the server is loaded, therefor nobody could touch any of the parts before the script loops through that part.

2 Likes

Also I have a quick question, if I were to put this inside a while true do loop, how much should I wait?

It doesnt work like that. Basically tbh the loops are very fast and there’s a 0% chance it would miss it

1 Like

Do not do that for the sake of your performance. This would graetly lag your game only because of tiny powerups.

I tried this on multiple text buttons in a game and it works perfectly since it’s a waste of time writing a script for each.

1 Like

The loop right now will only run once, it’ll then attach a function for whenever that part is touchd, therefor you’d have no reason to run it multiple times; if you want to detect when a new powerup is added to the folder then you can just check for ChildAdded

2 Likes

But then how would I constantly be checking if any powerup has been touched? If the for loop runs when the game starts it wont run no more because it is not being looped forever in a while true do loop.

Please try it this cud have ended a long time ago. And also make sure to check if the part that hits the part has humanoid object to confirm humanity.
Tbh i don’t know the logic behind it fully.
But what ik is that the loop ONLY runs if any of the children is touched.

1 Like

Touched event is constantly on ‘while true do’ when something touches brick it will start a function so make sure that a thing which touches this part has an ‘Humanoid’ in it, to make sure it’s player and not for example different brick.

1 Like

You should look into CollectionService, you can add tags to objects and get that tag table.
This dandy plugin can help with adding tags rather than having to apply them via a Script.

2 Likes