For loop ends prematurely even when adding to it while it loops

i’m working on a stim rn, that stim buffs you, like a lot

the stim by default lasts 10 seconds, but if you get a kill while it is active, an additional 2 seconds gets added into the timer

the problem is, the for loop will iterate through the default value, which is 10, even if the value is changed

local rStorage = game:GetService("ReplicatedStorage")

local events = rStorage:WaitForChild("Events")
local toClient = events:WaitForChild("ToClient")
local consumableEvents = toClient:WaitForChild("Consumables")

-- Events;
local berserkEvent = consumableEvents.BerserkEvent -- used in B3RS3RK

-- Table of functions;
local functionsTable = {
	Bandage = function(player: Player, consumable: Tool)
		local humanoid = player.Character.Humanoid
		
		humanoid.Health += 10
	end,
	
	B3RS3RK = function(player: Player, consumable: Tool)
		local berserkTheme = workspace:FindFirstChild("BerserkTheme")
		
		player:SetAttribute("Berserk", true)
		player:SetAttribute("BerserkTime", 10)

		player:SetAttribute("Walkspeed", player:GetAttribute("Walkspeed") * 2)
		player:SetAttribute("Sprintspeed", player:GetAttribute("Sprintspeed") * 2)
		
		player:SetAttribute("AttackSpeed", 2)
		player:SetAttribute("DamageMultiplier", 2)
		player:SetAttribute("DamageResistance", 0.2)
		
		berserkEvent:FireClient(player, berserkTheme)
		
		coroutine.wrap(function()
			for i = player:GetAttribute("BerserkTime"), 1, -1 do -- this will always go from 10 to 0, if the value gets increased in the middle of it, it doesn't detect that
				player:SetAttribute("BerserkTime", player:GetAttribute("BerserkTime") - 1)
				print(player:GetAttribute("BerserkTime")) -- this will always print the proper number
				task.wait(1)
			end

			player:SetAttribute("Berserk", false)
			player:SetAttribute("Walkspeed", player:GetAttribute("Walkspeed") / 2)
			player:SetAttribute("Sprintspeed", player:GetAttribute("Sprintspeed") / 2)	
			player:SetAttribute("AttackSpeed", 1)
			
			player.Character.Humanoid.Health -= 60
		end)()
	end,
}


-- Module itself;
local consumableFunctions = {}

function consumableFunctions.Consumed(player: Player, consumable: Tool)
	functionsTable[consumable.Name](player, consumable)
end

return consumableFunctions

incase you don’t believe me i add 2 to the attribute:

-- Functions;
	
	local function CheckBuffs()		
		if player:GetAttribute("Berserk") then
			player.Character.Humanoid.Health += 30
			
			player:SetAttribute("BerserkTime", player:GetAttribute("BerserkTime") + 2)
			if player.Character.Humanoid.Health > player.Character.Humanoid.MaxHealth then
				player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth
			end
		end
	end
	
	local function CheckForKill(humanoid: Humanoid)
		if humanoid.Health < 1 then
			CheckBuffs()
		end
	end
	
	local hitEnemies = {}
	
	local function DealDamage()
		local BOX_SIZE = meleeInfo[tool.Name]["BOX_SIZE"]
		local OFFSET = player.Character:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(0,0,-4)
		
		local BOX = workspace:GetPartBoundsInBox(OFFSET, BOX_SIZE)
		SwingSound:Play()
		SwingSound.PlaybackSpeed = 1
		stamina.Value -= COMBO_STAMINA_CONSUMPTION
		player.Regenerating.Value = false
		
		-- Check if any buffs are active;
		if player:GetAttribute("Berserk") then
			COMBO_DAMAGE = COMBO_DAMAGE * 2
		end
		
		for _,v in BOX do
			local otherPlayer = Players:GetPlayerFromCharacter(v.Parent)
			
			if v.Parent:FindFirstChild("Humanoid") and otherPlayer == nil and not table.find(hitEnemies, v.Parent) then
				table.insert(hitEnemies, v.Parent)
				print(hitEnemies)
				
				local enemy = v.Parent:FindFirstChild("Humanoid")
				enemy:TakeDamage(COMBO_DAMAGE)
				CheckForKill(v.Parent:FindFirstChild("Humanoid")) -- function gets called right here
				DamageIndicatorEvent:FireClient(player, COMBO_DAMAGE, enemy)
			end
		end
		hitEnemies = {}
	end

born to be a good coder

forced to write horrible code

i just replaced the for loop with a while loop lol

coroutine.wrap(function()
			while player:GetAttribute("BerserkTime") > 0 do
				player:SetAttribute("BerserkTime", player:GetAttribute("BerserkTime") - 1)
				print(player:GetAttribute("BerserkTime"))
				task.wait(1)
			end

			player:SetAttribute("Berserk", false)
			player:SetAttribute("Walkspeed", player:GetAttribute("Walkspeed") / 2)
			player:SetAttribute("Sprintspeed", player:GetAttribute("Sprintspeed") / 2)	
			player:SetAttribute("AttackSpeed", 1)
			
			player.Character.Humanoid.Health -= 60
		end)()

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