Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Instead of having a script in a part and copying, is there a way to reduce all the work needed?

What I mean is, is there a way to instead of having a script in a lava brick and copying it to other places I might want can I instead have one single script where it detects if it's touched.

like this

brick -script brick -script brick -script brick -script

is that a way to reduce this like:

brick brick brick brick Script(PARENT IS WORKSPACE)

2 answers

✓ Accepted answer

You can put all of your lava parts in one model and do like this in one script:

for i,v in next,Workspace.LavaParts:GetChildren() do
v.Touched:connect(function(hit)
--your code
end)
end

4 comments

0

ahh okay, but im a little confused what the 'next ' is?

0

It basically just iterates through everything inside of the table, and the :GetChildren() method creates a table of all of the children of a model.

0

why not just do pairs? isnt it the same? dont get me wrong, i like the answer just questioning the difference between these two commands.

0

pairs actually uses next, so it's more efficient to use next.

Yup! In fact, I even think this is better in terms of efficiency and CPU usage. But let's not get technical and let's answer your question.

function CreateLavabrick()
	local new = Instance.new("Part", game.Workspace)
	new.Name = "Laval"
	new.BrickColor = BrickColor.Red()
	new.Anchored = true
	new.Touched:connect(function(p)
		if p.Parent:FindFirstChild("Humanoid") then
			p.Parent.Humanoid.Health = -1
		end
	end)
	return new
end

-- For example, create 100 lava bricks and move them to a random location!

for i = 1, 100 do
	local lava = CreateLavabrick()
	lava.CFrame = CFrame.new(Vector3.new(math.random(-100,100), 2, math.random(-100,100))
end

This discussion is preserved as it was. New answers, comments, and votes are closed.