How do I organize this script

My scripts are really messy and I’m trying to learn how to organize them, how do I organize this?

local repstorage = game:GetService("ReplicatedStorage")

local module = require(workspace:WaitForChild("Weathers"))

local ongoing = false

local lightning = false

local damage = 0

function random()
	if ongoing then return end
	
	for i,v in pairs(module.Chances) do
		local ran = math.random(1,100)
		if ran <= v.Chance and not ongoing then
			ongoing = true
			local weather = module.weathers[v.Name]
			if weather.Combinable == true then
				ongoing = false
			end
			if weather.Clouds then
				local cloud = Instance.new("Clouds")
				cloud.Cover = 1
				cloud.Parent = workspace.Terrain
				game.Lighting.Atmosphere.Density = 0.4
			end
			if weather.Lightning then
				lightning = true
				local coroutine1 = coroutine.create(function()
					task.wait(weather.Duration)
					lightning = false
				end)
				coroutine.resume(coroutine1)
			end
		if weather.Damage > 0 then
			 damage = weather.Damage
		else
			damage = 0
		end
		repstorage.Speed.Value = weather.Speed
		repstorage.Texture.Value = weather.Texture
		task.wait(weather.Duration)
		if weather.Clouds then
			workspace.Terrain:FindFirstChild("Clouds"):Destroy()
		end
		repstorage.Texture.Value = "rbxassetid://0"
		ongoing = false
		game.Lighting.Atmosphere.Density = 0
		end
		end
end



while true do
	task.wait(0.05)
	coroutine.wrap(random)()
	if lightning == true then
		local ran = math.random(1,50)
		if ran == 2 then
			repstorage.Lightning:FireAllClients()
		end
	end
	for i,v in pairs(game.Players:GetPlayers()) do
		if not v.Character then 
			continue
		end
		v.Character.Humanoid:TakeDamage(damage)
	end
end

This script works with this module:

local weather = {}
local sound1 = game.SoundService["The Rain Song, Pt. 2"]

weather.weathers = {
	["Rain"] = {
		["Texture"] = "rbxassetid://671728795",
		["Clouds"] = true,
		["Duration"] = 180,
		["Combinable"] = false,
		["Lightning"] = false,
		["Damage"] = 0,
		["Speed"] = 100
	},
	["Snow"] = {
		["Texture"] = "rbxassetid://16471396163",
		["Clouds"] = true,
		["Duration"] = 180,
		["Combinable"] = false,
		["Lightning"] = false,
		["Damage"] = 0,
		["Speed"] = 20

	},
	["Thunder"] = {
		["Texture"] = "rbxassetid://0",
		["Clouds"] = true,
		["Duration"] = 180,
		["Combinable"] = true,
		["Lightning"] = true,
		["Damage"] = 0,
		["Speed"] = 100


	},
	["Hail"] = {
		["Texture"] = "rbxassetid://287441137",
		["Clouds"] = true,
		["Duration"] = 180,
		["Combinable"] = false,
		["Lightning"] = false,
		["Damage"] = 0,
		["Speed"] = 60


	},
	["AcidRain"] = {
		["Texture"] = "rbxassetid://131061775957144",
		["Clouds"] = true,
		["Duration"] = 180,
		["Combinable"] = false,
		["Lightning"] = false,
		["Damage"] = 0.2,
		["Speed"] = 200


	},
}

weather.Chances = {
	["Thunder"] = {
		["Name"] = "Thunder",
		["Chance"] = 1
	},
	["Rain"] = {
		["Name"] = "Rain",
		["Chance"] = 100
	},
	["Snow"] = {
		["Name"] = "Snow",
		["Chance"] = 1
	},
	["Hail"] = {
		["Name"] = "Hail",
		["Chance"] = 1
	},
	["AcidRain"] = {
		["Name"] = "AcidRain",
		["Chance"] = 1
	},
}

return weather

Dont name your module script ‘module’ name it what the name is, so in this case Weathers or WeatherModule.
For improved readability i would suggest naming the ‘ongoing’ variable to something like weatherOngoing and the ‘random’ function to spawnRandomWeather or randomWeather.
Make the script into smaller functions for even further improved readability, so, make a function for each weather event like lighting and cloudy weather like this:

local function spawnClouds()
	local cloud = Instance.new("Clouds")
	cloud.Cover = 1
	cloud.Parent = workspace.Terrain
	game.Lighting.Atmosphere.Density = 0.4
end

if weather.Clouds then
	spawnClouds()
end

In your module, you dont need to have chances separated from the weathers, just add chance as a property and you’ll have a shorter, better code. like this:

weather.weathers = {
	["Rain"] = {
		["Texture"] = "rbxassetid://671728795",
		["Clouds"] = true,
		["Duration"] = 180,
		["Combinable"] = false,
		["Lightning"] = false,
		["Damage"] = 0,
		["Speed"] = 100
		
		["Chance"] = 100 -- does not need to be in a separate table
	},

I also suggest taking a look in the roblox lua style guide, it taught me a lot and im sure it will teach you a lot too: Roblox Lua Style guide

1 Like