Adding Default Values to Function Inputs

I recently attempted make a terminal like GUI for my admin utilities. However, the roblox engine claims there is an error on line 12 function prt(text, foreground=nil, background=nil) - it expects a closed parentheses instead of the equal sign which is immediately after foreground. I tried changing nil to false, and adding spacing. Nothing works, and the code inside and before the function is completely fine as well. What on earth is wrong with the function header???

-- start of file
count = 1

function clear()
	for _, v in script.Parent.Lines:GetChildren() do
		if v.Name == "Line" and v:IsA("TextLabel") then
			v:Destroy()
		end
	end
	count = 0
end

function prt(text, foreground = nil, background = nil)
	-- ...function code is fine
end

this is the error you cant put a value inside the function variable

so then how do you add defaults?

You set them in the function if they’re nil (by default)

1 Like

does it not throw an error if you do not pass enough arguments?

Usually this would work in other languages like JavaScript, however for Lua, if you want them to define a specific class you would add a Colon to the function, something like this:

prt(text, foreground: nil, background: nil)
-- parameters are then assigned a class, which the interpreter will handle accordingly

Otherwise if you are looking to define a value thats missing, you’d have to define it within the function as was stated above. Usually you would do it first thing:

foreground: = condition and value or default -- assign value if condtion, otherwise default
background: = condition or default -- condition is met or default is given
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.