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

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

Asked by 5 years ago
Edited 5 years ago

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.

Here is the script:

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)

As you can see I am trapped in this situation. I don't know how to make it possible for the top function to be able to call the bottom function. Any type of help is appreciated!

0
im pretty sure you can call the function below anyways, even if it hasnt technically been declared yet. ive never done it, but try and see if it works Gey4Jesus69 2705 — 5y

3 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

You simply need to a forward declaration so that each function knows about the variable.

Example 1

-- declare variables
local Gui, startRound, timeLimit 

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

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

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

game.Players.PlayerAdded:Connect(startRound)

Though often you can reorder your functions to work around this problem.

You can also use a table to solve this.

local f = {}

f.Gui = function()
    -- Code here
    f.timeLimit() 
end

f.startRound = function()
    -- Code here
    f.Gui()
end

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

game.Players.PlayerAdded:Connect(f.startRound)

The problem is this will be recursive.

Hope this helps.

0
Thanks! StrategicPlayZ 58 — 5y
0
But wait... I thought it would work but it didn't. It says that passed value is not a function. StrategicPlayZ 58 — 5y
0
The code works for me. Check that this error is not from another script / line of code. User#5423 17 — 5y
0
Now it worked! Thanks! StrategicPlayZ 58 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

roblox needs to load the function in first but this is still possible:

local Gui = function()
    -- Code here
end

local startRound = function()
    -- Code here
end

local timeLimit = function()
    -- Code here
end

while wait(1) do
    -- put these in the order you want
    timeLimit()
    startRound()
    Gui() 
end

or if you are just firing it once:

local Gui = function()
    -- Code here
end

local startRound = function()
    -- Code here
end

local timeLimit = function()
    -- Code here
end

Gui()
startRound()
timeLimit()

hope this helped it my first ever post :)

0
but otherwise i dont think you can call a function from another function that's above sorry MrOpScripter 81 — 5y
0
I dont want to fire all of them at once. I want to fire one of them which will fire another which will fire another. StrategicPlayZ 58 — 5y
0
then just do in time limit code Start round and in that Gui MrOpScripter 81 — 5y
Log in to vote
0
Answered by 5 years ago

Try to put all your functions on the top, then the code that calls those functions below.

Answer this question