Weather system (Novice)

I created a randomly timed weather system for a baseball game my friend and I are working on. This is my first time creating any formal lua code, and I just want to know if it has any severe mistakes in it that could cause performance issues, etc. I would also like to know how this could be optimized and fit in to a smaller code block. Thanks in advance

The fog slowly fades in from the beginning fogend (5000) and the sound gradually fades in with the fog.

It works fine in game.

local model = script.Parent
local lighting = game:GetService("Lighting")
local raining = false
local sound = game.SoundService.Rain

spawn(function() 
	while true do
		wait()
		for i, v in pairs(model:GetDescendants()) do
			if v:IsA("ParticleEmitter") then
				v.Enabled = false
			end
			raining = false
		end
		wait(math.random(50,300))
		for i, v in pairs(model:GetDescendants()) do
			if v:IsA("ParticleEmitter") then
				v.Enabled = true
			end
			raining = true
		end
		wait(math.random(50,300))
	end

end)

while true do
	wait()
	if raining then
		sound.Playing = true
		sound.Volume = sound.Volume + 0.0025
		if sound.Volume >= 1.2 then
			sound.Volume = sound.Volume - 0.0025
		end
		if lighting.FogEnd > 1000 then
			lighting.FogEnd = lighting.FogEnd - 5
		elseif lighting.FogEnd <= 1000 then
			lighting.FogEnd = lighting.FogEnd + 1
		end
	elseif raining == false then
		sound.Playing = false
		sound.Volume = 0
		if lighting.FogEnd < 5000 then
			lighting.FogEnd = lighting.FogEnd + 1
		elseif lighting.FogEnd >= 5000 then
			lighting.FogEnd = lighting.FogEnd - 5
		end
	end
end

  1. GetDescendants & Pairs
    Your calling this function twice inside of a loop, what I would do is to initially store all of the partical_emmiters in a table, then add and remove them when a descendant enters/leaves the parenting of the model. IPairs is said to be faster than Pairs in roblox lua, which means you should use IPairs if you need to optimize it and its only using numbered indicies. Ipairs stops once theres a void in the table, or it reaches the end of the numbered indicies(from 1-inf), so using ipairs on :GetDescendants() and :GetChildren() is a more effecient way to go.
  2. += -= Expressions
    Using += & -=(*=, /=, %= etc.) on a variable will save text space instead of repeating the variable name twice
local emitters = {}

for _,obj in ipairs(model:GetDescendants()) do
	if obj:IsA('ParticleEmitter') then
		table.insert(emitters, obj)
	end
end

model.DescendantAdded:Connect(function(obj)
	if obj:IsA('ParticleEmitter') then
		table.insert(emitters, obj)
	end
end)
model.DescendantRemoving:Connect(function(obj)
	local found = table.find(emitters, obj)

	if found then
		table.remove(emitters, found)
	end
end)

spawn(function() 
	while true do
		wait()
		raining = true
		for _,emitter in ipairs(emitters) do
			emitter.Enabled = false
		end
		wait(math.random(50,300))
		for _,emitter in ipairs(emitters) do
			emitter.Enabled = true
		end
		raining = true
		wait(math.random(50,300))
	end
end)

while true do
	wait()
	if raining then
		sound.Playing = true
		sound.Volume += 0.0025
		if sound.Volume >= 1.2 then
			sound.Volume -= 0.0025
		end
		if lighting.FogEnd > 1000 then
			lighting.FogEnd -= 5
		elseif lighting.FogEnd <= 1000 then
			lighting.FogEnd += 1
		end
	elseif raining == false then
		sound.Playing = false
		sound.Volume = 0
		if lighting.FogEnd < 5000 then
			lighting.FogEnd += 1
		elseif lighting.FogEnd >= 5000 then
			lighting.FogEnd -= 5
		end
	end
end

1 Like
code
local lighting = game:GetService("Lighting")
local soundService = game:GetService("SoundService")

local model = script.Parent
local sound = soundService.Rain

local emitters = {}
local raining = false

function addEmitter(object)
    if object:IsA('ParticleEmitter') then
        table.insert(emitters, object)
    end
end

function enableEmitter(enabled)
    raining = enabled

    for _, emitter in ipairs(emitters) do
        emitter.Enabled = enabled
    end

    task.wait(Random.new():NextInteger(50, 300))
end

for _, object in ipairs(model:GetDescendants()) do
    addEmitter(object)
end

model.DescendantAdded:Connect(function(object)
    addEmitter(object)
end)

model.DescendantRemoving:Connect(function(object)
    local found = table.find(emitters, obj)

    if found then
        table.remove(emitters, found)
    end
end)

task.defer(function() 
    while true do task.wait()
        enableEmitter(false)
        enableEmitter(true)
    end
end)

while true do task.wait()
    sound.Playing = raining

    if raining then
        sound.Volume += 0.0025

        if sound.Volume >= 1.2 then
            sound.Volume -= 0.0025
        end

        lightning.FogEnd += (if lighting.FogEnd <= 1000 then 1 else -5)
    else
        sound.Volume = 0
        lightning.FogEnd += (if lighting.FogEnd < 5000 then 1 else -5)
    end
end

this is how I would go about the code

2 Likes