How can I call a function from a function which is below the function?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to call a function, where that function I am calling is below my function.

  1. What is the issue? Include screenshots / videos if possible!
local Gui = function()
    -- Code here
    timeLimit() -- It errors here
end

local startRound = function()
    -- Code here
    Gui()
end ```

local timeLimit = function()
    -- Code here
    startRound()
end

game.Players.PlayerAdded:Connect(startRound)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I couldn’t find one that has a similar questions to mine, or maybe I could not find it.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

function checkSight()
          follow() --I can call the function since it is below this function
end

function follow()
      checkSight() -- It errors, and I can't call the function since It is above?

      --Is there anyway I can call a function which that function I am calling has to be below?
end

In my script there are many functions. I want to call one function from the other, but the problem is that the function which will be calling another function is above the function it will be calling, and not below it so it is giving me errors. You might be saying to switch the places, but in my script there are a lot of functions, and if I switch the places of the functions then the other functions will have the same problem! Please tell me how I can make this work.

If you use a module script for your functions, you could call self which doesn’t care about order.
for example:

local module = {}

function module:checkSight()
   self:follow()
end

function module:follow()
   self:checkSight()
end

return module

This won’t error and will fire functions like expected.

3 Likes

They are functions it don’t matter the order you place them in the script other than they need to be defined before you call them. So just move it up over the one calling it …

local Gui
local startRound
local timeLimit
Gui = function()
    -- Code here
    timeLimit() -- It errors here
end

startRound = function()
    -- Code here
    Gui()
end ```

timeLimit = function()
    -- Code here
    startRound()
end

game.Players.PlayerAdded:Connect(startRound)
2 Likes

As long as you don’t call before definition (This):

local Gui
local startRound
local timeLimit
Gui = function()
    -- Code here
    timeLimit() -- It errors here
end

Gui() --Here I am calling before they are defined

startRound = function()
    -- Code here
end

timeLimit = function()
    -- Code here
    startRound()
end

Then there is no problem. For example this is completely fine:

local Gui
local startRound
local timeLimit
Gui = function()
    -- Code here
    timeLimit()
end

startRound = function()
    -- Code here
end

timeLimit = function()
    -- Code here
    startRound()
end

Gui() --This is fine since it is being called after the 3 functions were defined
3 Likes
local module = {}

function module.checkSight()
	module.follow()
end

function module.follow()
	module.checkSight()
end

return module

Worth mentioning that the ‘self’ is optional and the functions can be accessed directly through the module table itself.

1 Like