Wait() causing intense lag

This has been a reoccurring issue in my scripts. For some reason having a wait() of any kind will cause my game to lag out during the wait. It’s destroying my game and I don’t know why.

Example 1:

function Functions:Rain(Forecast)
   game.Lighting.Atmosphere.Density = .5
   RainingValue.Value = true    
   ClearDroplets()
   task.spawn(function()
       SpawnDroplets(Forecast)
   end)
   wait(Functions.Settings[Forecast].Duration * 60) --time before it stops
   RainingValue.Value = false
end

1. ```
local function ClearDroplets()
   task.spawn(function()
       for _,droplet in pairs(workspace.Raindrops:GetChildren()) do
           wait(.1)
           droplet:Destroy()
       end
       for _,droplet in pairs(workspace.Map.Plants:GetDescendants()) do
           if droplet:IsA("Part") and droplet.Shape == Enum.PartType.Ball then
               wait(.1)
               droplet:Destroy()
           end
       end
   end)
   
end

This was causing really bad lag, removing the cleardroplets() wait(.1) fixed it.

Example 2:
This script is in a module, it is called on as a reward system which then uses a function to spawns an orb the player can collect, the function can be used multiple times for each item that drops.

for i = 1,amount do
				spawnOrb(item,target)
			end


local function spawnOrb(item,target)
	local rarity = ItemInfoModule[item].Rarity
	
	local orb = ServerStorage.Orb:Clone()
	orb.Color = Colors[rarity]
	local light = orb.PointLight
	light.Color = Colors[rarity]
	local tagImage = orb.Tag.ImageLabel
	tagImage.Image = ItemInfoModule[item].ImageID
	
	local weld = Instance.new("Weld",orb.Box)
	weld.Part0 = orb.Box
	weld.Part1 = orb
	
	orb.Box.Position = target.Torso.Position + Vector3.new(0, 3, 0) -- Position the ball above the part
	orb.Box.Velocity = Vector3.new(math.random(-30, 30), 50, math.random(-30, 30)) -- Give the ball a random velocity
	orb.Parent = workspace
	
	local prompt = orb.ProximityPrompt
	prompt.ActionText = "Collect"
	prompt.ObjectText = item
	prompt.HoldDuration = collectionTimes[rarity]
	prompt.Triggered:Connect(function(player)
		orb:Destroy()
		basicFunctionModule:RewardItem(player,item,1)
	end)
	wait(.1)
end

with the wait, it causes tons of lag when the orb is spawned, when I take it out it fixes it entirely. (the wait is needed there isn’t a workaround for this one)

I’m not sure why this is happening. It’s not like I’m using it excessively.

1 Like
pcall(task.delay, Functions.Settings[Forecast].Duration * 60, task.defer, function() 
        RainingValue.Value = false
end)

I think you should task.wait instead of wait.
Wait is Deprecated.

What is the difference between the two?

1 Like

The only difference is that task.wait is more accurate. I have encountered this problem before, but I solved it after using task.wait

This is incorrect. wait() just is throttled, so task.wait() is more accurate.

I think what’s causing the issue if your use of asynchronous things here. Depending on how often these things are called, it can cause a lot of lag. An example is seen in ClearDroplets, which seems to have the only function of starting an asynchronous iteration thread.

Destroying and creating instances too quickly may also play a role in this.

It seems to be a deep issue. Could I have your discord by chance?

Completely off topic but what is the function reward item?

just a function that fires a remote to display an item they get

Is it something to do with oop? im a little new and i havent seen anyone do the symbol : with module scripts

I’d prefer not to go to Discord with this, if this topic is solved here it also may be useful in the future. Can you provide some more details? How often are these asynchronous threads called? How are they cleaned up? What do you mean by a deep issue?

@Dynamite2479 colon notation just defines self as the first parameter passed to the function, useful if tables need to reference themselves. OOP does use it, and it is more useful to OOP, but it is not limited to OOP. Here is some more information.

1 Like

I have two scripts for a rain system and a weather system, whenever I disable them it seems to stop the lag. But so does when I remove the waits.

The day system has 4 set times of day, it will shift to the different time of day using a tween every 8 minutes, which also tweening certain lighting aspects. This is done on the server, however I’m not sure if it would cause lag but it does run on a loop.

The rain system waits 15-20 minutes, then spawns droplets around the map. It shouldn’t really affect much of anything. It tweens a few lighting aspects like fog etc. but that’s it.

Could it be that for some reason my script memory or something is just being used up which causes the server to lag? I really don’t understand why it’s doing this. This is a continuation of my previous game and it’s heavily optimized, so too much space being taken is the last thing I’d expect, but…

With these scripts running it seems anything from spawning a character(NPC) to just spawning the parts in the script above, causes a FPS drop briefly.

So the script works fine without the waits? What about the amount of instances you have? I see each individual droplet is a part, that might be causing it.

Could I see some more code, such as what calls these functions and any other relevant info?

I mean they’re just ball parts that are anchored, they shouldn’t be causing any issues at all. Maybe a few hundred of them exist on the map.

Yeah, that’s going to take up a lot of memory having a few hundred of those on top of the other items within your game.

Can I see the parts of the script I asked about?

Just tested it and it’s actually 1700, on top of that there are about 100 rain droplets that will spawn. I get it’s a lot but they’re just ball parts. I have meshes in the hundreds on my map, would those not take more space than balls?

If you have a lot of individual instances on the map, it will cause lots of memory issues and these will stack up. Consider storing unused assets in ServerStorage, or consider using UnionOperations to help manage these assets.

i kind of need to see the script as well to help identify issues in there…

I’m not sure what script you’re wanting to see

these parts of the script, please