So, I’ve been watching these YT tutorials and they include functions. But I don’t know what to put inside the parentheses, and why they are placed there. What are their practical uses?
Depends on what you want to do.
One of the most common is generating a part because theres a lot of settings that must be done to modify the part in order to make it look good and what you want, size, position, CFrame, CanCollide, Anchored all that like the video AlvinBlox tutorial has done:
It can get really intense, here’s mine which generates a simple spherical part explosion for debugging purposes. I put the parameters to specify I want this explosion at this position and with this size. I do not care about anything else like the color since I want it all to be the same so I neglect to put that as the parameters.
Oh lawd it's long
local function createPartExplosionVisuals(position,blastRadius)
local radiusVisualization = Instance.new("Part")
radiusVisualization.Shape = Enum.PartType.Ball
radiusVisualization.Size = Vector3.new(blastRadius*2,blastRadius*2,blastRadius*2)
radiusVisualization.Position = position
radiusVisualization.Transparency = 0.5
radiusVisualization.BrickColor = BrickColor.Red()
radiusVisualization.Anchored = true
local radiusVisualization2 = Instance.new("Part")
radiusVisualization2.Shape = Enum.PartType.Ball
radiusVisualization2.Size = Vector3.new(blastRadius*1.5,blastRadius*1.5,blastRadius*1.5)
radiusVisualization2.Position = position
radiusVisualization2.Transparency = 0.5
radiusVisualization2.BrickColor = BrickColor.new(41)
radiusVisualization2.Anchored = true
local orangePart = Instance.new("Part")
orangePart.Shape = Enum.PartType.Ball
orangePart.Size = Vector3.new(blastRadius*1.25,blastRadius*1.25,blastRadius*1.25)
orangePart.Position = position
orangePart.Transparency = 0.5
orangePart.BrickColor = BrickColor.Yellow()
orangePart.Anchored = true
orangePart:SetAttribute("XRayIgnore",true)
radiusVisualization:SetAttribute("XRayIgnore",true)
radiusVisualization2:SetAttribute("XRayIgnore",true)
orangePart.CastShadow = false
radiusVisualization2.CastShadow = false
radiusVisualization.CastShadow = false
orangePart.Material = Enum.Material.SmoothPlastic
radiusVisualization.Material = Enum.Material.SmoothPlastic
radiusVisualization2.Material = Enum.Material.SmoothPlastic
PhysicsService:SetPartCollisionGroup(radiusVisualization2,"NonHittable")
PhysicsService:SetPartCollisionGroup(orangePart,"NonHittable")
PhysicsService:SetPartCollisionGroup(radiusVisualization,"NonHittable")
orangePart.Parent = workspace
radiusVisualization.Parent = workspace
local decayTime = 0.75
FastDebris(radiusVisualization,decayTime)
FastDebris(orangePart,decayTime)
FastDebris(radiusVisualization2,decayTime)
end
Now I can blow stuff up.
The parentheses in a function call have two purposes: they show that it’s a function call and you write the arguments to the function call inside them. There can be 0 or more arguments to a function call, each separated with a comma.
Some example function calls:
doSomething()
doSomething("aaaaaaa")
print("test")
print("test", 1)
Some functions take parameters, which are local variables inside the function that get whatever value is specified by the arguments in the function call. The first parameter gets the value of the first argument, the second parameter gets the second argument, etc. Function definitions also have parentheses, inside which the parameter list is written. The parameter list can have 0 or more comma-separated parameters, each of which is just any valid variable name.
Example functions:
function doSomething(parameter1)
print("I got " .. tostring(parameter1))
end
function isEven(number)
return number % 2 == 0
end
function biggestNumber(number1, number2)
if number1 > number2 then
return number1
else
return number2
end
end
The names of the parameters in the function definition are only visible and only used inside the function definition. That means the code that calls the function doesn’t need to know or worry about them.
If you want to figure out what the different parameters are to a function, you have two options: either look at the source code of the function to figure out what it does, or look at the documentation for the specific function. All the functions that are built into Roblox are fairly well documented on the wiki. E.g. if you want to know how to call the method (a type of function) Humanoid:MoveTo
, you search on the wiki and find this page: Humanoid | Roblox Creator Documentation
First there’s a description of what the function does, then a list of parameters so you know what to put in the parentheses of the function call.