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

how to fix error attempt to call a nil value?

mycode is



while true do
	wait(0.1)
	myfunction()
end





function myfunction()
	print("test")
end

but error is: attempt to call a nil value

pls help

2 comments

0

Move myFunction to be above the while loop. Your code is currently trying to call the function before it has been initialized

0

It's more like it never gets initialized. It would work fine if the code was in a different thread.

1 answer

The function must be placed before the loop as it has been undefined before the loop was made. In certain cases however, the function placement no longer matters. For example, you can call a function in a function that is undefined before the function.

Will work:

function greetGuys()
	hiGuys()
end

function hiGuys()
	print("hiGuys")
end
greetGuys()

Will not work:

while true do
	wait(1)
	foo()
end

function foo()
	print("foo")
end

This is because functions don't run the moment they are made like loops are since you have to call the function for it to run.

Your final script:

function myfunction()
	print("test")
end

while true do
	wait(0.1)
	myfunction()
end

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