Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

1 comment

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

3 answers

✓ Accepted answer

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.

4 comments

0

Thanks!

0

But wait... I thought it would work but it didn't. It says that passed value is not a function.

0

The code works for me. Check that this error is not from another script / line of code.

0

Now it worked! Thanks!

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 :)

3 comments

0

but otherwise i dont think you can call a function from another function that's above sorry

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.

0

then just do in time limit code Start round and in that Gui

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

This discussion is preserved as it was. New answers, comments, and votes are closed.