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!
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.
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 :)
Try to put all your functions on the top, then the code that calls those functions below.