Recently, I have been developing a fighting game. The problem is, over time, my servers keep building up lag. When that happens, usually my wait() times get extremely long. I’ve tried to make a custom wait() function, but all it did was cause more memory to be used.
This is an image from the game’s server list. I shut down all servers and recorded the time it took for the servers to lag. Not surprisingly, it took only 1 hour for them all to become extremely slow.
Since the core of the entire gameplay revolves around timed-effects, wait()s are used all the time.
local function AccurateWait(seconds)
if not seconds then
seconds = 0
end
local last = tick()
local secondsPassed = 0
local connection = RunService.Heartbeat:Connect(function(dt)
secondsPassed = secondsPassed + dt
end)
repeat
RunService.Heartbeat:Wait()
until secondsPassed >= seconds
connection:Disconnect()
end
I wasn’t sure if the Wait() function was the underlying problem. Everything in my code seems to work perfectly well. So I changed it to this, and it worked flawless for the first few minutes, but started to lag after that.
Also in my code I have coroutines running
I don’t use spawn(function(), instead I use a coroutine to create it’s own separate thread from the code.
So I would have
local loop = coroutine.create(function()
and then
coroutine.resume(loop)
However, I am pretty sure the coroutines wouldn’t be causing the lag. All loops I start in the code, I give it some specific condition for it to stop and break (yes, I checked every single one). And this lag started after I had already implemented the coroutines.
Also to mention, all of this code is in a ModuleScript, and it’s only required 2 different times by server scripts.
Yet this module script (SpellCasting) causes 50% of lag??
If anyone could give me any idea of where this lag could be coming from… please… I’ve been working to optimize the script for 3 days now and nothing is fixing it…