If/then/else not printing

Hey guys! I’m new to scripting with Roblox, and I’ve used the Lua coding book to code this, but I realized that with Roblox they use print instead of return. I put this into Studios and it won’t print or give an error. I’m not sure what happened, does anyone know how to fix this? Thanks!

function fact (n)
	if n == 0  then
		print(1)
	else
		print(n * fact(n-1))
	end
end

Is that the whole script? If so, you need to actually run the function. To do this, add fact(YOURNUMBER) at the end. Replace YOURNUMBER with a number

Make sure that you actually started the function by writing fact(Enter any number you want here) in the end of script as @Puzzled3d said.

Alright, I’ll try running that and see what I come up with.

Why did you put a space between the function name and the arguments?
It should be like this

function fact(n)
	if n == 0  then
		print(1)
	else
		print(n * fact(n-1))
	end
end

Do you mean like this? It won’t print still, the Lua book has return in it, I mistook what it meant, but am I missing something?

function fact(n)
	if n == 0  then
		print(1)
	else
		print(n * fact(n-1))
	end
end

fact(1)

Are you writing this in a Script or LocalScript?
Remember if it’s a local script you have to check the client output, else check the Server output.

Hello,
You cant multiply it with an undefined number, as you are rerunning the function inside the function, inside a print statement, please use something like this, since this above wont work.

function fact(n)
	if n == 0 then
		print(1)
	else
		print(n * (n-1))
	end
end

fact(1)

I’m using Script in Workspace.

That’s what I’ve tried, do you need to return the function or am I multiplying wrong?

function fact(n)
	if n == 0 then
		print(1)
	else
        local x = n - 1
        print(n * x)
	end
end

fact(1)

Yes, you need to return a value in the function, otherwise you are multiplying it with nothing, just add a return at the end, you could use this:

function fact(n)
	if n == 0 then
		print(1)
		return 1
	else
		local n2 = n * fact(n-1)
		print(n2)
		return n2
	end
end

fact(1)

Well thanks to you guys, both of those scripts worked! I seemed to have not given the variables a value.

Great! Please mark an answer as the solution so people know this has been solved.