How do I put a script in all parts without having to do it one by one?

Okay so im new to roblox game development and im currently making an obby with a lot of kill bricks. I know that theres a way to put a script in all parts without having to do it one by one by idk how to do it.

2 Likes

I suggest not to have a script for every part. CollectionService is much better in terms of organization. The video I’m appending explains the basics in under two and a half minutes.

Alternatively, you can place all “kill bricks” into a folder and iterate.

for _,brick in folder:GetChildren() do
end
3 Likes

make sure all the kill bricks are in a folder, then put this code inside a script in ServerScriptService

local killBricksFolder = workspace:WaitForChild'KillBricksFolder' -- Replace "KillBricksFolder" with the name of the folder
for i, v in pairs(killBricksFolder:GetChildren() do
	v.Touched:Connect(function(part)
		if part.Parent:FindFirstChild'Humanoid' then
			part.Parent.Humaoid.Health = 0
		end
	end)
end
1 Like
for i,v in pairs(workspace:GetDescendents()) do
if v.Name == "KillBrick" then
v.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then hit.Parent.Humanoid.Health = 0 end
end)
end
end

Two things i would suggest looking into here.

  1. Module Scripts - They can be used to run the same kind of code like a function but can be called from anywhere

  2. Corroutines - This essentially acts as another script running at the same time but gives you more control and the ability to compact scripts down so its all organised.

for this case i think just using corroutines would suffice and you can keep a table of corroutines for each of the obby parts that are scripted, this means you would only need 1 script for the entire obby.

Edit: Most people keep scripts in 1 of 3 places, in a folder in the workspace, in the script storage or in server storage. Personally I use a folder but Id reccomend using the script storage, As a rule of thumb the only time a script should be a descendant of an object is in the case of local scripts