Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to fix error attempt to call a nil value?

Asked by 4 years ago
Edited 4 years ago

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

0
Move myFunction to be above the while loop. Your code is currently trying to call the function before it has been initialized vector3_zero 1056 — 4y
0
It's more like it never gets initialized. It would work fine if the code was in a different thread. Fifkee 2017 — 4y

1 answer

Log in to vote
0
Answered by
aredanks 117
4 years ago

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
Ad

Answer this question