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

Attempted to call NIL Value error when I run this script?

Asked by 10 years ago
wait(5)
Start()

function Start()
    print("This function has ran!")
if game.Players.NumPlayers >= 2 then 
    Yes()
else
    No()    
end
end

function Yes()
    print("This function has ran!")
    wait(.1)
end

function No()
    print("This function has ran!")
    music = game.Workspace.HeartofCourage
    wait(1)
    music:stop()
    wait(5)
    music:play()
    music.Looped = true 
end

I have tried to fix it. This script is fricken' simple and it isn't working? Oh my god, imagine a complex script. Please tell me how I can fix this.

What it exactly says in the output window:

19:45:42.795 - Stack Begin
19:45:42.795 - Script 'Workspace.X', Line 2
19:45:42.796 - Stack End
0
In short, define your functions before calling them. GoldenPhysics 474 — 10y

2 answers

Log in to vote
1
Answered by
HexC3D 830 Moderation Voter
10 years ago

Overpride, your calling a function that has not been defined yet, in this case I'm going to re-organize your script.

function Yes()
    print("This function has ran!")
    wait(.1)
end

function No()
    print("This function has ran!")
    music = game.Workspace.HeartofCourage
    wait(1)
    music:stop()
    wait(5)
    music:play()
    music.Looped = true 
end

function Start()
    print("This function has ran!")
if game.Players.NumPlayers >= 2 then 
    Yes()
else
    No()    
end
end

wait(5)
Play()


You should define thing BEFORE you actually use them..

If the function is not defined before you call the function "Play()" , "Yes(), ","No()". Your going to get an error from the output saying that it is equal to nil.

0
Wow what an A+ answer, I definitely understood this one a lot better and it makes SENSE to me. Thanks for breaking it to someone who didn't know ;) Overpride 0 — 10y
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Function definitions are actually local variable assignments.

The following two snippets are equivalent:

local a = function()
    ...
end
function a()
    ...
end

Looking at your code, then, we see:

-- Line 2, source of error:
[Some use of Start]

-- Line 4, definition of Start

Which is clearly a problem, because you are using something before it was ever defined (using its default value of nil; specifically, calling its default value of nil because this is a function call).

The call to Start() has to occur after Start and and functions that Start references (Yes and No) have been defined, just like any code referencing variables has to be after the definition of those variables.

0
In other words he called a function that wasn't even defined yet so it's equal to nil , Lua is one wierd language o.e. HexC3D 830 — 10y
0
It's not weird, it's useful. Functions can be redefined on the fly, and also can be members of objects, so this behavior must exist somehow. BlueTaslem 18071 — 10y
0
I like this answer as well :) Thanks. Overpride 0 — 10y

Answer this question