Is there a way to make this function less laggy

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

im trying the make this cool effect of having a sphere fade into existence around the player

  1. What is the issue? Include screenshots / videos if possible!

The code ive written is VERY laggy, even on a high(ish) end PC, and i have 0 idea how to make it alot less laggy

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

things such as lowering the size works quiet easily, but that ruins the point of it in general, and since my question is so specific, theres no posts on it

.
.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

	local function Sphere(center, radius, numSegments)
		local workspace = game.Workspace
		local segmentAngle = math.pi / numSegments
		local fadewait = 0
		for theta = 0, math.pi, segmentAngle do
			local sinTheta = math.sin(theta)
			local cosTheta = math.cos(theta)

			for phi = 0, 2 * math.pi, segmentAngle do
				local sinPhi = math.sin(phi)
				local cosPhi = math.cos(phi)

				local x = radius * sinTheta * cosPhi
				local y = radius * cosTheta
				local z = radius * sinTheta * sinPhi

				local part = Instance.new("Part")
				part.Size = Vector3.new(1, 1, 1)
				part.Position = center + Vector3.new(x, y, z)
				part.CFrame = CFrame.new(part.Position,center)
				part.Anchored = true
				part.Transparency = 1
				part.Material = Enum.Material.Neon
				part.Color = Color3.new(0,0,0)
				part.Parent = workspace
				task.delay(fadewait,function()
					TS:Create(part,TweenInfo.new(1),{Transparency = 0}):Play()
				end)
			end
			fadewait+=0.1
		end
	end

	local centerPosition = HRP.Position
	local sphereRadius = 50
	local e = sphereRadius/10
	local segments = 32*e

	Sphere(centerPosition, sphereRadius, segments)

(some variables may need to be supplied yourself for testing)

im also taking other ways of making this effect entirely if there is any!
Thanks for any assistance

1 Like

Instead of creating a new part every time, why not use a set amount of parts? Not to mention based off the code itself, the parts are not being cleaned up.

1 Like

the function was jsut made and not beign put the use yet, so no need for the cleanign up part until its optomised.

and for the other one, its just some random math i used based off the original size to make it still fulyl complete the sphere

1 Like

That…doesn’t make sense to me.

As for the removing of the sphere, it’s simple to do. You just have to slightly rewrite your code.

local fadePart = TS:Create(part, TweenInfo.new(1), {Transparency = 0})
fadePart.Completed:Connect(function() part:Destroy() end)
fadePart:Play()
1 Like

as i said, the destroying is easy. but i cant put the function to use until its optimized.

and for re-explained the set number: the original scale was: Size = 10, Segments = 32.
and that made the sphere complete without any holes. so some simple math allowed me to make a sphere of a larger size, but still without any holes

1 Like
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local function Sphere(center, radius, numSegments)
	local workspace = game.Workspace
	local segmentAngle = math.pi / numSegments
	local parts = {}

	-- Pre-calculate positions
	local positions = {}
	for theta = 0, math.pi, segmentAngle do
		local sinTheta = math.sin(theta)
		local cosTheta = math.cos(theta)

		for phi = 0, 2 * math.pi, segmentAngle do
			local sinPhi = math.sin(phi)
			local cosPhi = math.cos(phi)

			table.insert(positions, {
				center + Vector3.new(
					radius * sinTheta * cosPhi,
					radius * cosTheta,
					radius * sinTheta * sinPhi
				),
				theta
			})
		end
	end

	-- Batch create parts
	for i, data in ipairs(positions) do
		local pos, theta = data[1], data[2]

		local part = Instance.new("Part")
		part.Size = Vector3.new(1, 1, 1)
		part.Shape = Enum.PartType.Block
		part.Position = pos
		part.CFrame = CFrame.new(pos, center)
		part.Anchored = true
		part.Transparency = 1
		part.Material = Enum.Material.Neon
		part.Color = Color3.new(0, 0, 0)
		part.Parent = workspace

		TweenService:Create(part, TweenInfo.new(1), {Transparency = 0}):Play()

		-- Spread load across frames
		if i % 10 == 0 then
			RunService.Heartbeat:Wait()
		end
	end

	return parts
end
local centerPosition = HRP.Position
local sphereRadius = 50
local e = sphereRadius/10
local segments = 32*e

Sphere(centerPosition, sphereRadius, segments)

batch creation, less parts and spreading across frames, this should work much better now

1 Like

this makes it quite alot better!
tho still pretty laggy, i think it’ll be fine for how im using it.
Though i dont really understand how it helps, its still really great.
Thank you!

2 Likes

Hey there! sorry for the late response, I’m currently at school. My code works on pre calculated positions, stored in a table, so if there is any lag it is typically in the period where the positions are being calculated. I avoided calculating positions and creating parts all at once to prevent lag and too much happening at once. My batch creation, which relies on the RunService.Heartbeat prevents overloading on the server and should only allow for 10 parts to be created per ‘heartbeat’. If you have any more questions on how it works i would be happy to answer them though, it was kind of sloppily written in my last 10 minutes before school so i can definitely help refine it in the future

1 Like

It worked great!
i did make a few adjustments so it didnt lag as bad and aswell as having it fade faster though.
(i set the parts size to 3 and reduced the part count, and i made the 10 parts into like 1000)

2 Likes

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