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()
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).
Technically, the right side (the name) is special; the left side is nonetheless a completely ordinary expression ↩
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.
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.
No, it is not possible. This is because there is a missing argument.