Should I Return Once a Condition Is Met?

I am making a keybind system that does a specific task to the server. However, I am unsure if I should return each time the keybind has already been met. Here is the code I have below:

RemoteEvents.keybindEvent.OnServerEvent:Connect(function(player: Player, input)
	if input == Enum.KeyCode.One then
		Ingame_Functions.equipWeapon(player.Character, primaryWeapon)
	end

	if input == Enum.KeyCode.Two then
		Ingame_Functions.equipWeapon(player.Character, secondaryWeapon)
	end

	if input == Enum.KeyCode.Three then
		Ingame_Functions.equipWeapon(player.Character, meleeWeapon)
	end
end)

Question: Should I return each time the condition has already been met? Would this improve my performance or am I wasting my time typing return for each keybind?

Theoretical answer:

RemoteEvents.keybindEvent.OnServerEvent:Connect(function(player: Player, input)
	if input == Enum.KeyCode.One then
		Ingame_Functions.equipWeapon(player.Character, primaryWeapon)
		return
	end

	if input == Enum.KeyCode.Two then
		Ingame_Functions.equipWeapon(player.Character, secondaryWeapon)
		return
	end

	if input == Enum.KeyCode.Three then
		Ingame_Functions.equipWeapon(player.Character, meleeWeapon)
		return
	end

It’ll save you a few nanoseconds. Once returned, the rest of the code won’t run and it wont check the other conditions, but you can just use elseif instead, it’ll do the exact same thing and look cleaner

1 Like

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