I’m trying to make a game that requires lots of parts, scripts and CPU usage, so I’m trying to give as much performance as possible for the player.
So I also need to find out which function is best, but I don’t know.
Here’s 2 possible functions I can have, but I need to know which one needs less power:
Script 1:
local old,new = humanoid.StateChanged:Wait()
if old == Enum.HumanoidStateType.Freefall or new == Enum.HumanoidStateType.Landed then
jumping = false
end
Script 2:
humanoid.StateChanged:Connect(function(old,new)
if old == Enum.HumanoidStateType.Freefall or new == Enum.HumanoidStateType.Landed then
jumping = false
end
end)
The first one yields the script to wait for the function to fire, and the second creates a connected function with the code inside.
If I were You, I would use the second script. I’m not sure about the first script - shouldn’t it be in an infinite while loop? If yes, thie means that the second option is better. On the other hand it doesn’t feel like a big difference.
Do you want to change jumping every time the character lands? If so, use Script 2.
Do you want to change jumping only a single time? If so, use Script 1.
However, I would still not use :Wait() if you want to do the latter because :Once() exists.
Connections are much prettier than yielding your script until you fire an event (there’s a chance that it doesn’t fire, and you will be infinitely yielding your script, although this probably shouldn’t happen in your case with Humanoid.StateChanged). The performance difference is wholly negligible and you should focus of optimizing your main systems instead of trying to find micro optimizations.
I would go the second function because if you add return after it then it would end also it looks easier to read which could be useful for debugging / any reorganizing
I would say the first one, assuming you wanted to listen for the change once, no connections are left behind in the first one thus you are saving memory.
There’s another one I have to know. Please find out this for me:
Script A:
if variable == false then return end
--Code continues
Script B:
if variable ~= false then
--Code continues
end
The first code stops the entire script if the variable if false, whereas the second just makes an if parameter to protect the code if the variable is false.
They are the same performance-wise, but use Script A. Guard clauses are much, much easier to understand and read than nesting all your code within if-statements.
You should not be asking yourself “Which one would be more performant?” when it comes to simple code architecture like this.
Yes but it has to be perfect, the performance. It’s already extremely high, so I’m trying to use as much shortcuts to my code as possible, still keeping it advanced.
Then you should turn your attention to optimizing whatever is causing your game to have performance issues instead. This is pretty much a waste of time and will do little to nothing to help solve your problem.
I suggest looking at the MicroProfiler to diagnose your issues.
If you have performance issues, you might need to look elsewhere because it is not comming from that simple function
You can use the performance tab to see script utilization (though it kind of sucks). Like @Content_Corrupted019 pointed out, the Micro Profiler will give you the most information about what is causing lag, but it’s also the hardest to read
Could we see the cpu time (from the ctrl f7 menu) or the performance tab?
(And the micro profiler if you know where to look at)