How to avoid repeating else statements

I’m making a combat system and I’ve noticed that I’ve been using so many repeated else statements I’m here looking for a better way to avoid making copy pasted else statements.

Code Snippet:

	if info.blockable then
		local isInFront = combatMod.CheckPosition(Char, VChar)
		warn("Front check: ", isInFront)
		if isInFront == "Infront" then
			if VChar.Blocking.Value == true then
				-- >> Parrying
				if tick() - VChar.LastBlock.Value <= _settings.ParryTime then
					local Stun = _G.Create("Stun", 1, VChar)
					local backVelocity = vRoot.CFrame.LookVector * -10
					addVelocity(backVelocity, 0.2, vRoot)
					
					VChar.LastBlock.Value = 0
					return "Parried"
				end
				-- << Normal blocking
				local backVelocity = vRoot.CFrame.LookVector * -20
				addVelocity(backVelocity, 0.3, vRoot)
				
				return "Blocked"
			else
				if info.stun then
					local Stun = _G.Create("Stun", info.stun, VChar)
				end
				dealDamage(VChar, vHumanoid, vRoot, info)
				return "Hit"
			end
		else
			if info.stun then
				local Stun = _G.Create("Stun", info.stun, VChar)
			end
			dealDamage(VChar, vHumanoid, vRoot, info)
			return "Hit"
		end
	else
		if info.stun then
			local Stun = _G.Create("Stun", info.stun, VChar)
		end
		dealDamage(VChar, vHumanoid, vRoot, info)
		return "Hit"
	end

For anyone curious about how the combat system is looking right now here’s a little gif (Animations are not mine)

To be honest I wouldn’t be that bothered but if you wish you can convert some repeated code into functions like

function Hit()
       if info.stun then
		local Stun = _G.Create("Stun", info.stun, VChar)
	end
 
       dealDamage(VChar, vHumanoid, vRoot, info)
       return "Hit"
end

Hit()

All of these return the same? Since return ends the function, you can drop all the else’s and just have this bit at the end of the function. It won’t run unless the ifs fail and you don’t return anything above this.

Otherwise your code looks quite acceptable. Code can’t always be pretty and somen parts will be repeated in coding. It can’t always be avoided without making the code worse in other areas. Just comment it, and use newlines occasionally to break up long chunks into sections. Put comments on their own lines, dedicated comment lines make both comments and code much easier to look at in a lot of cases.

if info.blockable and combatMod.CheckPosition(Char, VChar) == "Infront" and VChar.Blocking.Value then
	-- >> Parrying
	if tick() - VChar.LastBlock.Value <= _settings.ParryTime then
		local Stun = _G.Create("Stun", 1, VChar)
		local backVelocity = vRoot.CFrame.LookVector * -10
		addVelocity(backVelocity, 0.2, vRoot)

		VChar.LastBlock.Value = 0
		return "Parried"
	end
	-- << Normal blocking
	local backVelocity = vRoot.CFrame.LookVector * -20
	addVelocity(backVelocity, 0.3, vRoot)

	return "Blocked"
end

--[[The code below will run IF:
The attack is unblockable
The attack is blockable but the enemy is behind them
The attack is blockable and the enemy is infront but they are not blocking
]]

if info.stun then
	local Stun = _G.Create("Stun", info.stun, VChar)
end
dealDamage(VChar, vHumanoid, vRoot, info)
return "Hit"

P.S CheckPosition should really be returning a bool instead of a “Infront” and “Behind”.