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:
01 | local Gui = function () |
02 | -- Code here |
03 | timeLimit() -- It errors here |
04 | end |
05 |
06 | local startRound = function () |
07 | -- Code here |
08 | Gui() |
09 | end |
10 |
11 | local timeLimit = function () |
12 | -- Code here |
13 | startRound() |
14 | end |
15 |
16 | 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!
You simply need to a forward declaration so that each function knows about the variable.
Example 1
01 | -- declare variables |
02 | local Gui, startRound, timeLimit |
03 |
04 | Gui = function () |
05 | -- Code here |
06 | timeLimit() |
07 | end |
08 |
09 | startRound = function () |
10 | -- Code here |
11 | Gui() |
12 | end |
13 |
14 | timeLimit = function () |
15 | -- Code here |
16 | startRound() |
17 | end |
18 |
19 | 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.
01 | local f = { } |
02 |
03 | f.Gui = function () |
04 | -- Code here |
05 | f.timeLimit() |
06 | end |
07 |
08 | f.startRound = function () |
09 | -- Code here |
10 | f.Gui() |
11 | end |
12 |
13 | f.timeLimit = function () |
14 | -- Code here |
15 | f.startRound() |
16 | end |
17 |
18 | game.Players.PlayerAdded:Connect(f.startRound) |
The problem is this will be recursive.
Hope this helps.
roblox needs to load the function in first but this is still possible:
01 | local Gui = function () |
02 | -- Code here |
03 | end |
04 |
05 | local startRound = function () |
06 | -- Code here |
07 | end |
08 |
09 | local timeLimit = function () |
10 | -- Code here |
11 | end |
12 |
13 | while wait( 1 ) do |
14 | -- put these in the order you want |
15 | timeLimit() |
16 | startRound() |
17 | Gui() |
18 | end |
or if you are just firing it once:
01 | local Gui = function () |
02 | -- Code here |
03 | end |
04 |
05 | local startRound = function () |
06 | -- Code here |
07 | end |
08 |
09 | local timeLimit = function () |
10 | -- Code here |
11 | end |
12 |
13 | Gui() |
14 | startRound() |
15 | timeLimit() |
hope this helped it my first ever post :)
Try to put all your functions on the top, then the code that calls those functions below.