Spawned parts prematurely disappearing without request

I am creating a cloud generator that spawns clouds at a rate of x clouds per second (30 in my case) in one region, makes them move along the Z-axis, and then upon reaching a certain Z value, deletes them. The clouds utilize meshes. However, after about 1 minute, clouds start dissapearing even before reaching the designated Z-axis mark. 30*60 = 1800 clouds, which should not be a problem. However, I am still suspecting that the system is struggling and is unable to keep up. Therefore, the system may be deleting parts itself to save the game from crashing. I came here to confirm if this is the problem or if there is something in my script that I am overlooking, leading to the premature deletion of my clouds. If the system is deleting the clouds to maintain the integrity of the game, I would appreciate some advice on how to accommodate more clouds if possible.

The script for the cloud generation is below. GammaModule is just a module script I made which statistically gets a random number from any gamma distribution with a specified alpha and beta.

cloud generation script:

-- Framework created by Stickmaster, added features by Armenia1997	
local workspace = game:GetService("Workspace")
local lighting = game:GetService("Lighting")
local runService = game:GetService("RunService")
local generatorModule = require(game.ReplicatedStorage.GammaModule) 

limits = {Vector3.new(-3000, 200, 2900), Vector3.new(3000, 1000, 3000)}
local respawnZ = -500
local cloudsPerSecond = 30
local cloudName = "Cloud"				
local cloudsGroupName = "Clouds"
local rotatedClouds = true		

local rate = 1 / 30
local lastRelease = 0

local clouds = script.Parent:FindFirstChild(cloudsGroupName) or Instance.new("Model", script.Parent)
clouds.Name = cloudsGroupName

local function computeColor(altitude, avgSize)
	local ratio = ((altitude * 3) + avgSize) / 1000
	local color = ratio * 15 + 155
	if color > 255 then
		color = 255
	end
	return Color3.fromRGB(color, color, color)
end

local function computeYPos(size)
	local avgSize = (size.x / 3 + size.y + size.z / 3) / 2
	local yPos = math.random(limits[1].y, limits[2].y) + (math.random(1, 5) / 5) * avgSize
	return yPos
end

local function createcloud(pos)
	local cloneCloudName
	local randomCloud = math.random(1, 3)

	if randomCloud == 1 then
		cloneCloudName = "Cloud1"
	elseif randomCloud == 2 then
		cloneCloudName = "Cloud2"
	else 
		cloneCloudName = "Cloud3"
	end

	local cloneCloud = game.Lighting:FindFirstChild(cloneCloudName)
	local newCloud = (cloneCloud and cloneCloud:clone()) 
	if cloneCloud then
		local desiredMax = 4000
		local desiredMin = 100
		local alpha = 0.1
		local beta = 1
		local xSize = generatorModule.generateNumber(alpha, beta, desiredMin, desiredMax)
		local desiredMax = 200
		local desiredMin = 50
		local alpha = 4
		local beta = 1
		local elevate = 225 - generatorModule.generateNumber(alpha, beta, desiredMin, desiredMax)
		local zRan = math.random(1, 4)
		local zRanSign = math.random(1, 2)
		local zAxis
		if zRanSign == 1 then
			zAxis = xSize + math.random(0, math.floor((xSize * zRan) / 20))
		else
			zAxis = xSize - math.random(0, math.floor((xSize * zRan) / 20))
		end
		local size = Vector3.new(xSize, math.random(math.floor(xSize / 4), math.floor(xSize / 3)) + elevate, zAxis * 1.5)
		newCloud.Mesh.Scale = size
		pos = Vector3.new(pos.x, computeYPos(size), pos.z) 
		local avgSize = (size.x / 3 + size.y + size.z / 3) / 2
		local altitude = pos.y
		local color = computeColor(altitude, avgSize)
		newCloud.Color = color
	end

	newCloud.CFrame=CFrame.new(pos)
	if rotatedClouds then
		newCloud.CFrame = newCloud.CFrame * CFrame.Angles(0, math.pi * 2 * math.random(), 0)
	end
	local desiredMax = 50
	local desiredMin = 70
	local alpha = 100
	local beta = 1
	local altitude = pos.y
	local velocity = generatorModule.generateNumber(alpha, beta, desiredMin, desiredMax) + math.random(0, 5) * altitude / 600
	newCloud.Name = "CloudSpeed" .. velocity
	newCloud.Transparency = 0
	newCloud.CanTouch = false
	newCloud.CanCollide = false
	newCloud.Parent = clouds
end

local gc = clouds:GetChildren()

while true do
	local t1, t2 = wait(rate)
	local gc = clouds:GetChildren()
	if lastRelease < t2 then
		lastRelease = t2 + 1 / cloudsPerSecond  
		createcloud(Vector3.new(math.random(limits[1].x, limits[2].x), math.random(limits[1].y, limits[2].y), math.random(limits[1].z, limits[2].z)))
	end

	for i,v in pairs(gc) do
		local speed = string.sub(v.Name,11)
		v.CFrame = v.CFrame + Vector3.new(0, 0, -speed * rate)
		if v.Position.z < respawnZ then
			v:Destroy()
		end
	end
end

I’d really appreciate any help! If you need GammaModule to replicate the simulation, let me know!

I have a sneaking suspicion (:nerd_face:) that it may be due to a built-in performance limiter. Try opening performance stats or other tools which allow you to see how roblox is doing. Have you also tried testing this in an open roblox game?

I am not experienced with using performance stats in Roblox Studio. I took a look at the summary stats, and towards the end where a lot of clouds were populated, the FPS dropped to 19 frames per second, and the rendering would turn orange.

Is there anything specific I should look at or any other information that would be helpful, since I have never had to examine stats before? None of the previous games/minigames I am developing or working on are nearly as resource-intensive as this one.

Yeah, it is defo a performance issue then. Something I have tried in the past is using the resources in this thread:

Also, take a look at if it is really neccesary to have this many clouds / have them all active.

I’ll look into this later and will likely mark this as the solution! Thanks!

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