Assigning code to multple parts/children

How would I be able to write a script and then assign that script to say all the parts inside a folder or model? For example, a kill script when the player touches the brick. I can’t seem to find how to do this. Thanks :slight_smile:

1 Like

What I think you’re getting at is using Collection Service which is found here: CollectionService | Roblox Creator Documentation. What collection service does (I believe never really used it before) is you set tags to certain objects in which CollectionService will listen to those tags being set and add them to something like a class that sets how they behave. In your example, you can tag all the objects in a given folder to a class called Killpart (as an example) which kills the player on touch.

1 Like

I would do this for your example, but if you have the parts in different folders or models then I would use Collection Service like what @zayleak3 said.

local players = game:GetService("Players")
local folder = workspace.Folder --or model

for i, v in pairs(folder:GetChildren()) do -- goes throught the folder's children 
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			local plr = players:GetPlayerFromCharacter(hit.Parent)
			
			if plr then
				local char = hit.Parent 
				char.Humanoid.Health = 0
			end
			
		end)	
	end
end