As the title says, is it good to have a lot of print() throughout the whole script to show what the code executed? ( Kind of like seeing the progress )
for example
function something()
event:Fire()
print("Fired event")
end
VS
function something()
event:Fire()
end
( of course I mean scripts that have a lot of lines )
Yes, but I suggestly mainly when testing.
Having like 4 prints to be sure that something loaded client or server sided is good as well.
When I am confused why something is working I put 4 prints throughout my whole script A, B, C, D
When A does print and B doesnt I add more prints in between.
This is because errors and warnings only tell you so much.
Having prints isn’t an issue, but I probably recommend keeping the prints in the part that you think might bug in-game. But other than that, when I do something like
--// LocalScript
RemoteEvent:FireServer()
--// ServerScript
RemoteEvent.OnServerEvent:Connect(function()
print("Received from server!")
-- Other part of the code
end)
But after I’m done testing, I remove all the un-necessary print lines. That’s basically all I do…
Again, do not do like this:
RemoteEvent.OnServerEvent:Connect(function(player)
print("Received from server! Waiting 2 seconds..")
task.wait(2)
print("Waited 2 seconds! Firing back to client after 5 seconds..")
task.wait(5)
print("Firing client..")
RemoteEvent:FireClient(player)
print("Fired to client!")
end)
It’s does not look clean at all, instead you won’t even be able to find the line you’re looking for to change.
I would say use them where appropriate and keep them focused to what section of code is being tested. Though if you wish to use the print function multiple times, I believe that storing in the function in a variable improves speed (don’t quote me on that), like so:
local p = print
p("foo")
p("foo2")
All in all, having lots of print functions can unnecessarily clutter code and will slow your code, so for finalised publications, definitely consider removing them. I personally would avoid it, post-debugging, though this is personal choice.
I would suggest adding a new function. Use the runservice to check if the runtime is in studio or not. If it’s in studio, print it. That way you don’t have to remove them, and you can change the way you log stuff too.
local RunService = game:GetService("RunService")
local function Test(text)
if RunService:IsStudio() then
print(text)
end
end
Note: I made this script in visual studio, as I don’t have Roblox studio installed on this computer atm.
I use the print global all the time when I am testing my code. It is up to you if you want to see if an event fires or not. I personally only use print in the studio. Some people seem to suggest warn and error, but I would recommend to only use those for indicating risks and errors.