What are functions?

They still kind of confuse me. What are it’s uses for making games?

4 Likes

Functions are analogous to written procedures for a program. If a function is ran, the code will run in the specific order it was written in and operates as if you pasted the code in somewhere else. Useful for multiple uses with different variables from parameters.

3 Likes

Not sure if you’ve read this article before but definitely should explain Functions in depth.

1 Like

basically, its a way to “Store” code, bur that isn’t the best way to describe it.

lets say, if something hits a part you want to print something, but you also want it to happen when a players joins, you would use the same function, here’s what I mean:


function MyFunction()
    print("I Exist!!")
   -- can have other lines here
end

workspace.Part.Touched:Connect(MyFunction) -- runs the code in the function when the part gets touched

game.Players.PlayerAdded:Connect(MyFunction) -- runs the code in function when a player joins

3 Likes

Very simple explanation if you mean in code:

Instructions that can be run for a purpose and give back an output of nil or something if specified in the instructions during its run.
(nil is a value type that you can associate with nothingness)

local function sum(a, b) -- its purpose is to give you a value for example
    return a+b
end
1 Like

Functions are very useful and makes life a lot easier when programming. Take the below code for example, where it does a check for an item called “Thing” in an inventory.

local Inventory = {}

local IsInInventory = false
for _, Item in ipairs(Inventory) do
    if Item.Name == "Thing" then
        IsInInventory = true
        break
    end
end

if not IsInInventory tthen
    -- Code
end

Instead of doing that, we could use a function that returns a bool whether the inventory contains the item or not.

local Inventory = {}

local function IsInInventory(ItemName)
    for _, Item in ipairs(Inventory) do
        if Item.Name == ItemName then
            return true
        end
    end
    return false
end

if not IsInInventory("Thing") tthen
    -- Code
end

Functions are also great because they can be ran multiple times, vs without one you’ll be copying and pasting the same/similar code over and over. Using the above code again as an example, we can set up the conditional statement to check for “Thing1”, “Thing2” and “Thing3” using IsInInventory, rather than setting up multiple variables/for loops to check for them.

local Inventory = {}

local function IsInInventory(ItemName)
    for _, Item in ipairs(Inventory) do
        if Item.Name == ItemName then
            return true
        end
    end
    return false
end

if not (IsInInventory("Thing1") and IsInInventory("Thing2") and IsInInventory("Thing3")) tthen
    -- Code
end

I hope this helped. :slight_smile:

3 Likes

You should try to rewatch the tutorial again and again to understand how it works.

Basically, functions are pre-defined codes which are stored inside it and it allows us to execute a specific code over and over again whenever we want to call it. This is extremely useful as it can be code organizing and saves you a lot of space.

1 Like

I think I understand it a bit now, but what’s the “_” in your code? What does it do

2 Likes

An underscore (“_”) is usually used when a value’s unused. For example, the above for loop usually returns two values: the index and the value it’s assigned to. However, since the index is unused, I used an underscore in place. Technically you can use it, but it’s used to hold a value you’re not going to use. (Hopefully that makes sense)

1 Like