Faster MeshPart Creation System

So I’m currently making a project which involves spawning “hampters”, involving lots and lots of mesh part creation. Here’s the video:

The problem is it’s very slow for my application, cuz my eventual plan is one “hampter” will summon per 1 / P seconds, where P is the number of players.

Currently I’m using AssetService:CreateMeshPartAsycn() in this code:

function MT.Methods:Create(position: Vector3, parent: Instance?): MeshPart
	local self = self :: Hampter
	local mesh = AS:CreateMeshPartAsync(self.MeshId) -- This is the problem
	local click = Instance.new("ClickDetector")
	local TweenFo = TweenInfo.new(2)
	
	mesh.Anchored = true
	mesh.Name = self.Name
	mesh.Size = Vector3.new(self.Size, self.Size, self.Size)
	mesh.Color = self.Color
	mesh.Transparency = 1
	mesh.Reflectance = self.Reflectance
	mesh.Material = self.Material
	if self.TextureID then mesh.TextureID = self.TextureID.Uri end
	mesh.Position = position
	mesh.Parent = parent or nil
	
	--Tween In
	local Sparks = Instance.new("Sparkles")
	Sparks.SparkleColor = self.Color
	Sparks.Parent = mesh
	local TweenIn = TweenService:Create(mesh, TweenFo, {Transparency = self.Transparency})
	TweenIn:Play()
	
	task.wait(2)
	
	mesh.Anchored = false
	Sparks:Destroy()
	click.Parent = mesh
	
	task.spawn(function()
		local isClicked = false
		local eT = os.clock() + self.LifeSpan
		
		if self.Special then task.spawn(function() self.Special(mesh) end) end
		
		click.MouseClick:Once(function()
			isClicked = true
			print(`{self.Name} - {self.Variant.Name}: ${string.format("%.2f", self.Price)}`)
		end)
				
		while os.clock() < eT and not isClicked do task.wait() end 
		
		local Sound = Instance.new("Sound")
		Sound.Parent = mesh
		if not isClicked then
			click:Destroy()
			Sound.SoundId = "rbxassetid://8090403894"
			
			local Smoke = Instance.new("Smoke")	
			Smoke.Color = self.Color
			Smoke.Parent = mesh
			
			while not Sound.IsLoaded do task.wait() end
			
			local TweenOut = TweenService:Create(mesh, TweenFo, {Transparency = 1})
			TweenOut:Play()
			Sound:Play()
			task.wait(2 + 0.5)
		else
			Sound.SoundId = "rbxassetid://8973091827"
			
			while not Sound.IsLoaded do task.wait() end
			
			Sound:Play()
			task.wait(Sound.TimeLength + 0.5)
		end

		mesh:Destroy()
	end)
	
	return mesh
end

I tested every functions, this by itself runs for ~0.5 seconds. The AS:CreateMeshPartAsync() part itself takes ~0.25 seconds to run…

I’ve seen in the MeshPart Documentation that I could also use MeshPart:ApplyMesh(). However the problem with that is I would have to store some assets in the ServerStorage and also it’s not future proof because my plan is to create more meshes for more varieties in the future.

So I’m thinking either creating more threads as a “wait” while it spawns more but Idk I’m just a beginner.

You know, being a good developer means you put some effort into making and ensuring your games work like they should. Use :Clone() bro, avoid asyncs when possible

1 Like

Instance.fromExisting() is more fitting here actually.

1 Like

Hmm, I guess you’re right actually. I think the better approach is maybe by using AssetService:CreateMeshPart() to replicate the meshes to ServerStorage, then using the MeshPart:ApplyMesh() or Instance:Clone(). That way it keeps the future proofing and it also runs much faster once every asset loads, thx for giving me an idea man.

Well I got the thing to run and now on average it takes about ~0.00032s to run now, thx guys.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.