How to enable/disable print statements in a script globally

i want some print statements to be displayed only when a variable is true, the method i think of right away is doing:

if var then print('something') end

as you can see, this gets a a little annoying when you have like 4000 lines of code, is there a way to disable/ enable print statements through a variable without having to add the if statements??

I don’t know if I understood you’re question right (I apologize in advance if I did), but try formatting the in then statement like this so that you can fit the rest of the code.

if var then
print('something')
--lines of code
end

Explanation


You can basically just make one function to print all values and check in debounce in one place.

Instead of adding the if statement everywhere just call the function and enter your arguments.

This method is also really handy for debugging code with a switch inside the game.

Print Method


Create your own print method instead:

local printEnabled = true

function Print(argument)
    if printEnabled then
        print(argument)
    end
end


Print("Hello World")

Warn Method


Create your own warn method instead:

local warnEnabled = true

function Warn(argument)
    if warnEnabled then
        warn(argument)
    end
end


Warn("Error Message")

No need to modify any of your existing code, or create your own print with a slightly different name, simply override the print global by shoving in your own copy which respects your toggle.

local DEBUG_PRINTS = true

local oldprint = print
local function print(message)
	if DEBUG_PRINTS then
		oldprint(message)
	end
end

print("hello")
3 Likes

Put a boolean in serverStorage, and using this name:

we can name it DEBUG_PRINTS

Now in a script:

(function()
	local oldprint = print

	print = function(message)
		if game.ServerStorage.DEBUG_PRINTS.Value then
			oldprint(message)
		end
	end
end)()

(You only need this in one script to affect all scripts)

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