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

Pipe lining multiple functions?

Asked by 9 years ago

Is is possible in Lua to put multiple functions together?

Here is an example if I were regenerating a vehicle. This is what I would normally do:

refrence = game.ServerStorage:FindFirstChild(vehicleName)
vehicle = refrence:Clone()

But, is it possible to do it stuff like this?

vehicle = game.ServerStorage:FindFirstChild(vehicleName):Clone()
2
As long as "vehicleName" is defined, then it is kind of possible. Since the method ":FindFirstChild()" returns the object if it finds it, or "nil" if it doesn't find it, then if the method returns "nil", nothing will be cloned. Redbullusa 1580 — 9y

4 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Yes. Assignment isn't something special in Lua; the right side of the = is just an expression. Neither is . or : special; the left side is just an expression1.

Thus you can of course use them together!


However, in the particular code you mentioned, you lose something when you do this. :FindFirstChild is allowed to return nil. If it did return nil you would get an error when you try to ask for how to :Clone nil. Assigning it to a variable gives you a chance to make sure it exists by using that variable in an if.

If you don't expect the object to not exist, it's briefer and clearer to not use :FindFirstChild, and just use normal indexing using square braces:

local vehicle = game.ServerStorage[vehicleName]:Clone()

If you aren't familiar with this, you might have seen something like this in code like

workspace.BlueTaslem["Left Arm"]:Destroy()

The .name is actually just sugar for ["name"] -- table indexing by a string. If we want a variable, you just put the variable in the square braces (without quotes -- it's not text).


  1. Technically, the right side (the name) is special; the left side is nonetheless a completely ordinary expression 

Ad
Log in to vote
2
Answered by
yumtaste 476 Moderation Voter
9 years ago

Yes. You can do that. Just make sure to write the functions correctly. Also, that variable will be a clone of an item, not a clone of an item that you could use multiple times. That part is difficult to explain, but I'm sure you know. If you have any further questions, feel free to comment.

Log in to vote
1
Answered by 9 years ago

You can, but you need to be careful to avoid errors. For instance, in your example, if FindFirstChild returned nil, then attempting to call ":Clone()" will error because "nil:Clone()" is not valid.

This sort of error could occur without chaining, but you'd normally make sure that the values aren't nil before proceeding:

reference = game.ServerStorage:FindFirstChild(vehicleName)
if not reference then return end --if reference is nil, return from whatever function we're in
vehicle = reference:Clone()

However, if you're positive that the model exists, there's nothing stopping you from doing this:

gameServerStorage[vehicleName]:Clone().Parent = workspace

Of course, if you need to call :MakeJoints on that model, you'll need to save the result of "Clone" to a variable so that you can access it more than once.

You can do this in general by creating functions that return the object they work on. ex:

function SetParent(obj, parent)
    obj.Parent = parent
    return obj
end
function MakeJoints(obj)
    obj:MakeJoints()
    return obj
end
function ColorModel(obj, color)
    for _,p in ipairs(obj:GetChildren()) do
        if p:IsA("BasePart") then
            p.BrickColor = color
        end
        ColorModel(p, color)
    end
    return obj
end

SetParent(ColorModel(MakeJoints(game.ServerStorage["MyModel"]:Clone()), BrickColor.Random()), workspace)

--Or with tabs to make it easier to read:
SetParent(
    ColorModel(
        MakeJoints(
            game.ServerStorage["MyModel"]:Clone()),
        BrickColor.Random()),
    workspace)

If you want to use the colon to join custom functions, you'll need to use inheritance, which gets much more complicated.

Log in to vote
-3
Answered by
Invisum -5
9 years ago

No, it is not possible. This is because there is a missing argument.

Answer this question