Better Function

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.

2 Likes

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.

2 Likes

The first script isn’t a loop, it just yields the script until the function fires, using :Wait()

1 Like

Im not expert at it, but I think second one is better, because you create a variable in the first one.

You are right

1 Like

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.

To answer your question, use Script 2.

1 Like

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

1 Like

The function perfectly works on both scripts, I just want to know which one has better performance.

1 Like

Use the second one to make your code easily understandable. What would affect performance is the code you’re running when the event is fired.

So everything including and after

if old == Enum.HumanoidStateType.Freefall or new == Enum.HumanoidStateType.Landed then
	jumping = false
end

will dictate the performance of your script.

1 Like

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.

1 Like

The change isn’t listening for it once, it is in a loop. I just showed a section of that code I wanted to make performance-based.

1 Like

If the loop has a heartbeat:Wait() or task.wait() the first one is the same as the second, performance-wise.

1 Like

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.

Hard to tell without more context but these would be the same. If there is another end after the 2nd if statement then it terminates the scope.

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.

3 Likes

So can I get a short explanation to what that is?

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.

I don’t see how one or the other can make any noticable difference.
Both use events, the cpu usage from either one will be extremly low

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)