hi, I have a question, what are the best ways to handle the lag? Anything like creating many pcalls and connection. And what are bad practices at scripting? Anything like using deprecated things
Well for lag, there is a script performance tab, you can see the activity of your scripts there. If any activity is >= 3% it might be worth optimising.
For pcall, that is for exception handling. Use it to handle web requests like data store requests, http requests etc. should be wrapped in pcall.
As for bad practices there are many, either roblox-exclusive or not. If you think you might be following a bad habit use the code review category to get feedback on your code
This is a bit of a generalized question that doesn’t reach into enough specifics but, to give you a few tips I will recommend “code smells” that you should avoid;
Using lots of wait(). Instead you should use :WaitForChild(), or event:Wait(), consider the following example
while not player.Character do wait() end
character = player.Character -- Bad practice
character = player.CharacterAdded:Wait() -- Good practice
Another common one is using for loops and delays instead of TweenService. When I was first getting started I did this a lot and it hurt my games performance quite a bit, but now that I use TweenService things are a lot smoother and only use a percentage of the CPU they did before. Example :
for i=1,100 do
wait(0.05)
part.Transparency = i/100
end -- Not necessarily bad but, if used a lot in conjunction with other things it can get messy (and even laggy)
game:GetService("TweenService"):Create(part,TweenInfo.new(5),{Transparency=1}):Play()
-- ULTRACOMPACT (not recommended but gets the job done fast if you speak fluent spaghetticode
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(5)
local goal = {Transparency = 1}
local tween = tweenService:Create(part,tweenInfo,Goal)
tween:Play()
-- Sane version ;)
TL;DR for basic optimization, use TweenService instead of for loops when you can, and use events instead of while loops when you can.
Also, this discussion here has some good points. Hope all this helps!
https://devforum.roblox.com/t/what-are-your-roblox-specific-code-smells/427709
Threads about performance and whatnot come up so frequently on the forums, it would do you well to read up on those kinds of threads. I made a long post about performance before, I would encourage giving it a read through.
This post focuses more on things that can affect performance though, rather than how to avoid or handle cases of lag and there’s not much on practice there. It’s within the same scope though.