For loop is extremely slow for some reason

I have come across a very weird bug that I don’t know how to fix, so I was making some tools for my game, and when I went to test them, the timer was running very slow even though it has a wait of 1 second. Here’s a quick module script I made:

local GameModule = {}

local rep = game:GetService("ReplicatedStorage")
local status = rep:WaitForChild("TimerTime")


function GameModule.Countdown(number)
    for i = number, 0, -1 do
        status.Value = i;
        warn("Counting down...")
        wait(1)
    end
end


return GameModule

It prints(“Counting down”) after like 3-4 seconds. I don’t know how and why this happened. Thanks for any help that solves this problem. :slight_smile:

3 Likes

How complex is this place?
I’ve noticed that wait() becomes unreliable the more memory a place is using.

If the place is large, try cutting down the amount of scripts and the amount of times larger modules are required.
If it’s smaller, it might be a bug.

2 Likes

The game doesn’t have that many scripts, though the game has many coroutines in 1 script.

Can coroutines cause that?

1 Like

I’m not sure on that one, but if they are all loops constantly doing an action with a small wait() time, I’d say that it’s likely to be the problem.

Yeah. They are all loops doing an action with a small wait… I think that’s my problem right there?

I assume coroutines could definitely cause that, but I don’t know if it’s that significant to cause such amount of lags. As @Irideon already pointed out, there is no issue that can be found in your code, and the most liked reasoning is lags. Can you screenshot how much PlaceMemory your game has?
Also, in the Developer Console, go to ‘Memory’ and inspect it, see what takes a lot of MB, and if possible, try to use this element less.

I’m currently joining the game and I’ll send a screenshot when I am in.

1 Like

try use a custom wait but again, if the server lags then also yielding extends

local rs = game:GetService("RunService")
local function Wait(s)
     local b = 0
     while b<s do b += rs.Heartbeat:Wait() end
end


Here

I see that there is a lot of usage of memory when it comes to replicating stuff.
How often do remote events fire in your game?
I need the answer alike: Every _ seconds

1 Like

It’s a story game. What do you expect?

They fire every 5-6 seconds

It’s not good for performance, that is a really possible problem as I do not find any issue in your code.
I suggest optimizing your game, and try to use less Remote Events, as they require the player’s network in order to complete the communication.

Alright, I will try to optimize the game and possibly get the memory leak to around 300.

Thank you, and @Irideon for the help.

1 Like

If you’d like some tips, here there are (when Remote Events are not necessary):

  • Change walkspeed/jumppower on the client.
  • Change the position of a character on the client (as it is automatically replicated).
  • Play animations on the client (as it is automatically replicated).

There are more things. When you make something, test it on the client and see if it replicates, if it doesn’t and it is a necessary thing for your game, use a Remote Event.
Good day. :pray:

3 Likes