Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
yurhomi10 192
11 years ago

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

Log in to vote
2
Answered by 11 years ago

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

1for i,v in next,Workspace.LavaParts:GetChildren() do
2v.Touched:connect(function(hit)
3--your code
4end)
5end
0
ahh okay, but im a little confused what the 'next ' is? yurhomi10 192 — 11y
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. TheGuyWithAShortName 673 — 11y
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. yurhomi10 192 — 11y
0
pairs actually uses next, so it's more efficient to use next. TheGuyWithAShortName 673 — 11y
Ad
Log in to vote
0
Answered by
jobro13 980 Moderation Voter
11 years ago

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.

01function CreateLavabrick()
02    local new = Instance.new("Part", game.Workspace)
03    new.Name = "Laval"
04    new.BrickColor = BrickColor.Red()
05    new.Anchored = true
06    new.Touched:connect(function(p)
07        if p.Parent:FindFirstChild("Humanoid") then
08            p.Parent.Humanoid.Health = -1
09        end
10    end)
11    return new
12end
13 
14-- For example, create 100 lava bricks and move them to a random location!
15 
16for i = 1, 100 do
17    local lava = CreateLavabrick()
18    lava.CFrame = CFrame.new(Vector3.new(math.random(-100,100), 2, math.random(-100,100))
19end

Answer this question