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

Why are the functions failing to be called?

So this script is supposed to call on functions when certain values are made true. However, it doesn't work, and I have no idea why. Output has been saying the functions I am trying to call are nil. Can someone please help me?

Here's the script:

local Playing = script:WaitForChild("Playing")
local Done = script:WaitForChild("Done")

while wait() do
if Playing.Value == true then
if Done.Value == true then
End()
wait(3)
ReDo()
end
end
end

function End()
print('Game over')
end

function ReDo()
print('Restarting')
end

2 comments

0

Check if the values are true. Instead of doing if Playing.Value == true then, do if Playing.Value then. Shortcuts!

0

Output said that the functions "End()" and "ReDo()" are nil. Forgot to add that.

1 answer

✓ Accepted answer

That's because they are nil.


Your script is being read left to right, top to bottom. Therefore when you call the function, the function doesn't exist yet because you haven't created it. You will actually never create it, since you put it after a while that will loop forever. Even though you type out the code, that code will never be read nor will it be executed.

Just create the function before you get sucked into an endless loop.

--Tab your code correctly!

local Playing = script:WaitForChild("Playing")
local Done = script:WaitForChild("Done")

function End()
	print('Game over')
end

function ReDo()
	print('Restarting')
end

while wait() do
	if Playing.Value == true then
		if Done.Value == true then
			End()
			wait(3)
			ReDo()
		end
	end
end

Might as well combine the if statements and shorten the conditions;

--Tab your code correctly!

local Playing = script:WaitForChild("Playing")
local Done = script:WaitForChild("Done")

function End()
	print('Game over')
end

function ReDo()
	print('Restarting')
end

while wait() do
	if Playing.Value and Done.Value then
		End()
		wait(3)
		ReDo()
	end
end

3 comments

0

Thanks!

0

You could also shorten them even more by doing: local Playing, Done = script:WaitForChild("Playing"),script:WaitForChild("Done")

0

Yes, but using tuples like that leads to very unreadable and messy code.

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