Best way to Unload and load in terrain without lag?

Currently, I’ve noticed that about 1-4 players leave the game when the match is over and the map starts to unload, and teams all spectator. They might be crashing, can’t be sure. But still, players do say they experience lag when this happens. It’s understandable since there are about 20-30 players being teamed all spectator while the map is being removed. They only experience the lag when this is happening, about 2-5 seconds.

Is there a possibility that I can reduce the lag for the players, so they can enjoy it better?

This is the code sample:

-- loads in terrain
workspace.Terrain:PasteRegion(game.ServerStorage.Storage.Terrain[MapA.Name], workspace.Terrain.MaxExtents.Min, true)

-- After the match, it starts to remove map and team all spectator.

	for i,v in pairs(game:GetService("Players"):GetPlayers()) do
		if v ~= nil then
			local success, err = pcall(function()
				v.Team = game:GetService("Teams"):FindFirstChild("Spectator")
			end)
			local successs, errr = pcall(function()
				v:LoadCharacter()
			end)
			wait()
		end
	end
	wait(1)
	-- END
	workspace.Terrain:Clear()

I understand that 2-5 seconds of lag every 15 minutes isn’t a big deal, but I still would like some input, much thanks! :slight_smile:

2 Likes

It will be something with the fact that terrain takes a while to load, try doing the “generate terrain” function to have a feel on how slow terrain is due to high poly/tri count

did you put this script in server storage?

Have you found any workaround for this so far?

Just out of curiosity, how do you save, store and load terrain? I’ve been trying to get this to work for a while and never really got a fully functioning system ;-;