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
10 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 10 years ago

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
0
ahh okay, but im a little confused what the 'next ' is? yurhomi10 192 — 10y
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 — 10y
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 — 10y
0
pairs actually uses next, so it's more efficient to use next. TheGuyWithAShortName 673 — 10y
Ad
Log in to vote
0
Answered by
jobro13 980 Moderation Voter
10 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.

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

Answer this question