Massive lag issue as the game progresses [Solved]

The FPS continues to drop as I play my game while the ping rises. I believe it may be one of 2 reasons as I am a new scripter. Every time I animate an attack, I load the animation first, not sure if it could be because of loading too many tracks on the humanoids or something instead of preloading them? The more likely reason is my script which deals with cooldowns for player attributes like if they are able to block or not in combat. I don’t want the cooldowns to overide each other so I made this script that creates a new event, the script has int values inside of them that represent the time I want to cooldown to last :

local detectors = script.Parent

--Adds the event to all timers

local OldValues = {
CanAttackOld = 0,
CanBarrageOld = 0,
CanBlockOld = 0,
CanDashOld = 0,
CanDeflectOld = 0,
CanEquipOld = 0

}

local ticks = {
	
CanAttackTick = 0,
CanBarrageTick = 0,
CanBlockTick = 0,
CanDashTick = 0,
CanDeflectTick = 0,
CanEquipTick = 0

}

for i,v in pairs(script:GetChildren()) do
	
	
	
	---------------------------------------------------------------------------------------------------------------
	
	--Event that checks when timer starts
	v.Changed:Connect(function(change)
	
		local timer = v
		local detector = script.Parent:FindFirstChild(v.Name)
		local continueRunning = true
		local startRunning = true
		
		--Makes sure that the change is an actual change and not the timer being reset
		if v.Value ~= 0 then
			
			--Turns the detector off
			detector.Value = false
			
			---------------------------------------
			
			--Gets the tick that the previous timer started running at as well as the value of the old timer
			
			local oldValue
			local oldTick
			
			--Timer value
			for i,v in pairs(OldValues) do
				
				if i == timer.Name.."Old" then
					
					oldValue = i
					
				end
				
			end
			
			--Tick value
			for i,v in pairs(ticks) do
				
				if i == timer.Name.."Tick" then
					
					oldTick = i
					
				end
				
			end
			
			---------------------------------------
			
			--The function that starts a new timer
			local function  BeginTimer ()
				
				local TimeToWait = timer.Value --Stores the timer value so that it can be reset to 0, awaiting a new timer
				ticks[oldTick] = tick() --Sets the value of the oldTick variable
				OldValues[oldValue] = timer.Value/1000 --Sets the value of the oldTimer variable
				timer.Value = 0 --Resets the timer so that if an identical tiemr to the previous is called, the change will be registered
				
				--Checks if the timer is changed a second time
				timer.Changed:Connect(function()
					
					--Prevents any small timers from stopping a bigger one
					if (timer.Value/1000) > (OldValues[oldValue] - (tick() - ticks[oldTick])) then
					
						continueRunning = false --Tells the current timer if it should continue to run or not
					
					end
					
				end)
				
				--The timer
				task.wait(TimeToWait/1000)
				
				--Checks if this timer is still valid
				if continueRunning == true then
					
					--Turns the detector back to true
					detector.Value = true
					
				end
				
			end
			
			---------------------------------------
			
			--Ensures that a small timer within a bigger timers range will not run
			if (timer.Value/1000) > (OldValues[oldValue] - (tick() - ticks[oldTick])) then
			
				BeginTimer()
			
			end
			
		end
	
	end)
	
end

I am guessing this is why my game is lagging because of the constant creation of new events but I want to be sure so that I can resolve the issue. Some tips to fix it if you know would be lovely too.

your connecting a timer.changed event, then connecting the same .changed again, that means if the timer changes the second change you connected will run and the main change is also fired and if the conditions are then one more second .changed will be connected, this will stack and evetually lag

1 Like

Ok so I was right, thanks just making sure that was the reason. :+1:

1 Like

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