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

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

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

1 comment

0

In short, define your functions before calling them.

2 answers

✓ Accepted answer

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.

1 comment

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

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.

3 comments

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.

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.

0

I like this answer as well :) Thanks.

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