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

How to call a function that hasn't been made yet?

so i have 3 functions

function startGame()
	if script.Running.Value = -1 then
		endGame()
	end
end

function preGame()
	startGame
end

function endGame()
	preGame()
end

i want the end game to work but roblox says "endGame is nil on line 3" how would i fix this?

4 comments

0

Since you are using a round system based game I would suggest organizing your variables, functions etc.. in a module. They are more efficient. Organizing your functions like you did is not a good idea, since you are going to use a ton of functions the script will be so messy and you will suffer when udating your code.

0

alright thanks, it works now

0

You could use _G

0

what is that?

1 answer

There's actually nothing wrong with the orientation of the functions. If nothing calls the functions, it just iterates over the functions and loads them, what's wrong is this:

function preGame()
	startGame
end

the "startGame" does not have a function bracket thingie which prevents endGame() from being defined. Although I'd imagine the script would 100% break before any function could run in it.

Anyways, I recommend a set up like this:

function startGame()
	f script.Running.Value = -1 then
		endGame()
	end
end
 
function preGame()
    startGame()
end
 
function endGame()
    preGame()
end

startGame() -- e.g. this

Run any type of function AFTER all the above functions (shown in the example above), so the functions get defined and you won't get the error. You can never call a function that hasn't been made since it'd be "nil"

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