Wait() taking too much time

Hello, in my game Scuba Diving in Deep Waters Roleplay [ATLANTIS] - Roblox

the wait() takes wayy too much time. And for some reason all the loops with wait() are not fluid. My game contains alot of Terrain but i have Content Streaming Enabled. The chunks are supposed to load in and load out, keeping the client memory low and allowing players with weaker devices to play.

  • Any way i can fix this?

Thanks for reading

2 Likes

Are you saying that your game is lagging, if so look for things that may lag it for instance random scripts with nothing in and humanoids lying around in the workspace. If not try bringing down the Content Streaming settings.

Hope this helps.

1 Like

The weird thing is, the Server seems to lag but not the players. I have no idea and i have cleared random scripts & useless debris already

1 Like

Hmmm, that is strange, it may either be a slow server or your internet / connection to the server as if you are using wait(), then it should not take ages.

The wait() seems like a wait(0.8)
Its sort of delayed, and all the waits are kinda broken.

  • This only happened to me in this game.

It is for sure not my internet connection

1 Like

When using wait() you shouldn’t expect it be right on time. You’re telling your script to wait for the given amount of time and then some extra until it can find a spot in the scheduler to continue running. Meaning the busier your scheduler gets the more likely you risk inaccurate times.

My best advice is to look for other methods of handling your loops and try to find spots where event based triggers can be used.

7 Likes

This means that, for example if i have 20 scripts with wait() loops running at the same time, it will cause to slow down the server?

Thanks for your reply, ill look into that thread and keep you updated

1 Like

RenderStepped, Heartbeat is quicker than wait()

2 Likes

Yes the general principle is the more you call wait() in a period of time the more inaccurate it gets. So having 20 loops using it can really bog down your script performance.

If you really need a loop you can use RunService.Heartbeat:Wait(). But you should try and find ways to avoid loops in general.

1 Like

i checked all my scripts, i only have a few using wait()
Some use wait(3) but i think that should be fine.

Most of my Server Scripts are script.Parent.ChildAdded:Connect(function() type functions

1 Like

Are you using CollectionService or other methods to combine multiple but similar game items into one script?

Since script.Parent suggests each of these items has its own script?

1 Like
local function WaitCustom(n)
      local RunService = game:GetService("RunService")
      n = n or 0.001
      if n >= 1000 then n = 1000 end
      if n <= 0.001 then n = 0.001 end
      local Time = os.clock()
      repeat
              RunService.Heartbeat:Wait()
      until (Time - os.clock()) >= n
end

This is called a “heartbeat wait”. I think it should perform better.

5 Likes

I think the issue is somewhere else. I don’t have that much Wait Loops running at a time.

  • Maybe this is caused by the amount of terrain?
1 Like

Use os.clock() instead of tick(). It’s supposed to be deprecated eventually.

1 Like

Oh! I never knew about that! (Also, I was thinking in the future, Unix epoch time could get very high, so maybe deprecation might be the right step)

The Voxel Terrain wouldn’t cause lag on the server. I’ll hop on your game and try and find anything.

Thanks! ill be looking in Studio

You could use renderstepped with ROBLOX’s RunService.

For the time issue you could use os.time or tick to get the exact second

Avoid using RenderStepped unless it is something very very important.
RenderStepped blocks rendering until the connected function is completed. So if you put anything demanding in there, your game will fall apart.

Here is a chart by rogchamp: image

Unless it is physics or camera related, don’t use renderstepped or stepped. Use Heartbeat.

1 Like