Very repetitive code in stamina system

As you can see this script is very repetitive, between the two events. What can I do to make it more efficient?

local StaminaDeductionEvent = game.ReplicatedStorage.StaminaDeduction

local hitEvent = game.ReplicatedStorage.HitEvent

local checkStaminaMod = require(game.ReplicatedStorage.ModScript1)

hitEvent.OnServerEvent:Connect(function(player, otherPart)
	local humanoid = otherPart.Parent.Humanoid
	local damage = player.leaderstats.Damage
	local strengthAmount = player.leaderstats.StrengthMax.StrengthAmount
	local strengthMax = player.leaderstats.StrengthMax
	local add1 = 0.02
	local add2 = 0.04
	local chances = math.random(0,100)
	
	local function checkStamina()
	local hitReduce = false
	local staminaMax = player.leaderstats.StaminaMax
	if player.leaderstats.StaminaMax.StaminaAmount.Value <= staminaMax.Value / 2 then -- 50%
		local staminaMax = player.leaderstats.StaminaMax.Value / 2
		if player.leaderstats.StaminaMax.StaminaAmount.Value <= staminaMax / 2 then -- 25%
			return hitReduce == true
		else
			return hitReduce == false
		end 
		
		else
			return hitReduce == false
	end
end
	local staminaMaxx = player.leaderstats.StaminaMax
	local staminaAmount = player.leaderstats.StaminaMax.StaminaAmount
	local hitReduce = checkStamina()
	
	if hitReduce then
		humanoid:TakeDamage(damage.Value/2)
	elseif not hitReduce then
		humanoid:TakeDamage(damage.Value)
	end

	if chances <= 50 and strengthAmount.Value < strengthMax.Value then
		strengthAmount.Value = strengthAmount.Value + add1
	elseif chances >= 51 and strengthAmount.Value < strengthMax.Value then
		strengthAmount.Value = strengthAmount.Value + add2
	end
end)

StaminaDeductionEvent.OnServerEvent:Connect(function(player, otherPart)

local function checkStamina()
	local canBlock = true
	local staminaMax = player.leaderstats.StaminaMax
	if player.leaderstats.StaminaMax.StaminaAmount.Value <= staminaMax.Value / 2 then -- 50%
		local staminaMax = player.leaderstats.StaminaMax.Value / 2
		if player.leaderstats.StaminaMax.StaminaAmount.Value <= staminaMax / 2 then -- 25%
			return canBlock == false
		else
			return canBlock == true
		end 
		
		else
			return canBlock == true
	end
end

	local staminaMaxx = player.leaderstats.StaminaMax
	local staminaAmount = player.leaderstats.StaminaMax.StaminaAmount
	local canBlock = checkStamina()
	
		if canBlock then
			print('Can Block!')
			local oppName = otherPart.Parent.Name
			local oppAccName = tostring(oppName)
			local opp = game.Players:FindFirstChild(oppAccName)
			staminaAmount.Value = staminaAmount.Value - 10 -- TEST
			--below is formula for percentage
			--local oppStrength = opp.leaderstats.StrengthMax.Value/2
			--staminaAmount.Value = staminaAmount.Value - oppStrength
			--local percent = staminaAmount.Value / staminaMaxx.Value * 100
		end
		
		if not canBlock then
		print('Can not block!')
		wait(4)
	end
end)
1 Like

I’d recommend bringing the checkStamina function out of the scope of just the event connection, and instead, just providing the parameters for the variables it uses.

1 Like

Yes, but how do I grab the specific player if it’s out of each of the events?

You’d want to pass it as a parameter. Here’s an example:

local function checkStamina(player)
    -- check stamina and all that
end

hitEvent.OnServerEvent:Connect(function(player, otherPart)
    local stamina = checkStamina(player)
end)

Interesting, I tried that before but it seems I forgot to add ‘player’ into calling the function, thank you!