How do I make this less laggy + speed it up?

Basically it randomly selects platforms that are different, however the platform with difference causes lag, and its high chances because of :MoveTo
How would I do this more efficiently?


tohidefromnewplayer = {}




local module = require(game.ServerStorage.RandomGen)


local row = 0
local num = -1
while true do
	task.spawn(function()
		local new
		num = num + 1;
	local chances = module.GenerateRandom()
	if chances == nil then
		new = game.ServerStorage.Plots["Empty Area"]:Clone()
		end
		task.spawn(function()
	if chances ~= nil then
		local find = game.ServerStorage.Plots:FindFirstChild(chances)
		if find ~= nil then
				new = find:Clone()
				local s=module.returncomp(chances)
				--print(s)
				if s == true then
					--print("Ok!")
					new.Parent = workspace.RandomlyGenPlots;
					game.ReplicatedStorage.Remotes.HideUntilNearby:FireAllClients(new,Vector3.new(-985, 0.5, -985) + Vector3.new(65 * num, 0, 65 * row))
					table.insert(tohidefromnewplayer,find)
				end
				if s == false then
					new.Parent = workspace.RandomlyGenPlots;
				end
		end
			end
		end)
		task.spawn(function()
				new:MoveTo(Vector3.new(-985, 0.5, -985) + Vector3.new(65 * num, 0, 65 * row))
		end)
		task.spawn(function()
	if num >= 30 then
		row = row + 1;
		num = -1 
		end
		end)
	end)
	if row >= 32 then
		game.Lighting.WaitingForPlotLoad.Value = true
		break
	end
	wait()
end

The first thing you could do is reduce the number of task.spawn calls you make. I don’t know what kind of performance gains you could get from it, but you don’t need most of them.

Do you know what code is executed when you run module.GenerateRandom() and module.returncomp()?

I’ll remove a few of the task.spawns

Modules;


_common = {
	"Empty Area";
}
_uncommon = {
	"Empty Area";
}
_rare = {
	"Hardware Area";
}

_extremelyrare = {
	"Empty Area";
	"Building Area #1";
	"Building Area #2";
}
_dreamluck = { -- Therapist
	"Empty Area";
	
}

comparison = { -- GET HIDDEN BY RANGE?
	[true] = "Empty Area";
	[true] = "Building Area #1";
	[true] = "Building Area #2";
	[true] = "Hardware Area";
}

function randomgen.returncomp(AreaName)
	for v,i in pairs(comparison) do
		if i == tostring(AreaName) then
			return v
		end
	end
end



function randomgen.GenerateRandom() -- // Might redo these chances for later ¯\_(ツ)_/¯
	local chances = math.random(1,4000)
	if chances >= 100 and chances <= 700 then
	--	print("Common!")
		return _common[math.random(1,#_common)]
	end
	if chances >= 701 and chances <= 1150 then
	--	print("Uncommon!")
		return _uncommon[math.random(1,#_uncommon)]
		
	end
	if chances >= 1151 and chances <= 1400 then
		--print("Rare!")
		return _rare[math.random(1,#_rare)]
	end
	if chances >= 1401 and chances <= 1415 then
	--	print("Extremely rare!")
		return _extremelyrare[math.random(1,#_extremelyrare)]
	end
	if chances >= 1416 and chances <= 1417 then
	--	print("Dream Luck!")
		return _extremelyrare[math.random(1,#_extremelyrare)]
	end
	if chances >= 3 and chances <= 99 then
	--	print("Common!")
		return _common[math.random(1,#_common)]
	end
	if chances >= 1 and chances <= 2 then
	--	print("Dream Luck!")
		return _dreamluck[math.random(1,#_dreamluck)]
	end
	if chances >= 1418 and chances <= 1520 then
	--	print("Rare!")
		return _rare[math.random(1,#_rare)]
	end
	if chances >= 1521 and chances <= 1580 then
	--	print("Rare!")
		return _rare[math.random(1,#_rare)]
	end
	if chances >= 1581 and chances <= 2320 then
		--print("Common!")
		return _common[math.random(1,#_common)]
	end
	if chances >= 2321 and chances <= 2340 then
		--print("Extremely rare!")
		return _extremelyrare[math.random(1,#_extremelyrare)]
	end
	if chances >= 2341 and chances <= 2500 then
		--print("Uncommon!")
		return _uncommon[math.random(1,#_uncommon)]
	end
	if chances >= 2501 and chances <= 2800 then
		--print("Rare!")
		return _rare[math.random(1,#_rare)]
	end
	if chances >= 2801 and chances <= 3000 then
		--print("Uncommon!")
		return _uncommon[math.random(1,#_uncommon)]
	end
	if chances >= 3001 and chances <= 3400 then
		--print("Uncommon!")
		return _uncommon[math.random(1,#_uncommon)]
	end
	if chances >= 3401 and chances <= 3600 then
		--print("Uncommon!")
		return _rare[math.random(1,#_rare)]
	end
	if chances >= 3601 and chances <= 3700 then
		--print("Uncommon!")
		return _rare[math.random(1,#_rare)]
	end
	if chances >= 3701 and chances <= 3703 then
	--	print("Uncommon!")
		return _extremelyrare[math.random(1,#_extremelyrare)]
	end
	if chances >= 3704 and chances <= 3790 then
	--	print("Uncommon!")
		return _extremelyrare[math.random(1,#_extremelyrare)]
	end
	if chances >= 3691 and chances <= 4000 then
	--	print("Uncommon!")
		return _common[math.random(1,#_common)]
	end
end

I would reduce the amount of task.spawn's and replace them with a coroutine. Also, use task.wait() instead of wait().

I’ve now reduced the amount of task.spawns, and replaced it with a task.wait(), but there isn’t much of a difference, the FPS still has issues being stable.

1 Like

Given that module code, you don’t need any of those task.spawn calls since all of the code is synchronous.

You also have a malformed comparisons table in your module; only the last key, "Hardware Area" will be there. You should just turn the keys into the values and values into keys.

It also looks like you’re cloning an area every frame, which sounds like the source of the lag. Is that behavior intended?

I’m trying to make it generate a grid with different things in it, I’ll fix the table though.

Not much of a difference was spotted, how do I make this more efficient?