Better way to organize mass amount of variables?

Hi! I do not know what type of support this should be filed under, but I feel as if there may be a way to do something about this related to scripting so I’m going to ask under scripting support. For my game, I have a VFX handler that sends all the VFX to each persons client for each ability which is great! But with each ability comes new VFX and with new VFX comes a lot of new variables…

--Services
local RS = game:GetService("ReplicatedStorage")
local PLRS = game:GetService("Players")
local TS = game:GetService("TweenService")
--Modules
local hitboxModule = require(RS:WaitForChild("HitboxCreator"))
local cameraModule = require(RS:WaitForChild("ScreenShake"))
--VFX Folders
local animsFolder = RS:WaitForChild("Animations")
local sfxFolder = RS:WaitForChild("Sound Effects")
local vfxFolder = RS:WaitForChild("Effects")
local eventsFolder = RS:WaitForChild("Events")
local abilityRemote = eventsFolder:WaitForChild("AbilityRemote")
--Slam Directory
--Remotes
local slamEvents = eventsFolder:WaitForChild("SlamEvents")
local slamVFX = slamEvents:WaitForChild("SlamVFX")
local slamValidator = slamEvents:WaitForChild("SlamValidator")
local playSlamVFX = slamEvents:WaitForChild("PlaySlamVFX")
--Animations
local slamAnim = animsFolder.SlamAnims:WaitForChild("Slam")
--Effects
local slamRocks = vfxFolder["Slam Effects"]:WaitForChild("SlamRocks")
local slamExplo = vfxFolder["Slam Effects"]:WaitForChild("Explosion")
--Sounds
local whoosh = sfxFolder.SlamSounds:WaitForChild("Whoosh")
local slamImpact = sfxFolder.SlamSounds:WaitForChild("GroundSlam")
--Wow that's quite a few variables!

slamVFX.OnClientEvent:Connect(function(plr)
	
	print("Connected to VFX Handler!")
	
	local pChar = plr.Character
	local slam  = pChar.Humanoid.Animator:LoadAnimation(slamAnim)
	
	slam:Play()
	whoosh:Play()

	slam:GetMarkerReachedSignal("Knockback"):Connect(function()
	
		local vTable = hitboxModule.CreateHitbox(pChar, 0, 0, 0, 15, 15 ,15)
		slamValidator:FireServer(pChar, vTable)
				
		local pChar = plr.Character
		local humanoid = pChar:FindFirstChild("Humanoid")
		local hrp = pChar:FindFirstChild("HumanoidRootPart")
		local pDirection = hrp.CFrame.LookVector
				
		local victimVFX = playSlamVFX.OnClientEvent:Connect(function(validHit)
			
			cameraModule.smallShake(pChar, 0.5)
			slamImpact:Play()
			
			local rocksClone = slamRocks:Clone()
			local primaryRock = rocksClone.PrimaryPart

			for _, effect in pairs (slamExplo:GetChildren()) do
				local effectClone = effect:Clone()
				effectClone.Parent = primaryRock
			end
			
			local rockTweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)
			local rockTweenProperties = {
				["CFrame"] = hrp.CFrame + Vector3.new(0,-3,0)
			}
			local rockTween = TS:Create(primaryRock, rockTweenInfo, rockTweenProperties)

			rocksClone:PivotTo(hrp.CFrame + Vector3.new(0,-5,0))
			rocksClone.Parent = workspace

			rockTween:Play()
			
			game.Debris:AddItem(rocksClone, 1.2)
			
			if #validHit == 0 then --If nobody was hit this will fire and then stop the script
				--print("Nobody was hit!")
				return
			else
				for _, victim in pairs (validHit) do --Any victim related effects go here

				end
			end
		end)
		
		task.delay(3, function()
			victimVFX:Disconnect()
		end)
	end)
end)

Not that I think having a lot of variables is a problem, it’s going to happen if you have a lot of different abilities with different VFX. My question though pertains to asking if there is a way to put all of the variables somewhere else or organize them a bit more? Sort of like a script of a global library with all of my commonly used variables that can be pointed to from anywhere. I just don’t want my VFX Handler to become a jumbled mess of a bunch of variables if I can do something about it.

if u have questions on game structure & development put it in Game Design Support :slight_smile:

u could make a global config module script with all ur VFX variables, services, sfx, etc and require it when u need those variables. (yes i had to use AI for this question… i want to learn as well don’t judge me :zipper_mouth_face:)

global config module would look something like this:

-- GlobalConfigModule.lua
local RS = game:GetService("ReplicatedStorage")

local config = {}

-- VFX Folders
config.animsFolder = RS:WaitForChild("Animations")
config.sfxFolder = RS:WaitForChild("Sound Effects")
config.vfxFolder = RS:WaitForChild("Effects")
config.eventsFolder = RS:WaitForChild("Events")

-- Slam Events
config.slamEvents = config.eventsFolder:WaitForChild("SlamEvents")
config.slamVFX = config.slamEvents:WaitForChild("SlamVFX")
config.slamValidator = config.slamEvents:WaitForChild("SlamValidator")
config.playSlamVFX = config.slamEvents:WaitForChild("PlaySlamVFX")

-- Animations
config.slamAnim = config.animsFolder.SlamAnims:WaitForChild("Slam")

-- Effects
config.slamRocks = config.vfxFolder["Slam Effects"]:WaitForChild("SlamRocks")
config.slamExplo = config.vfxFolder["Slam Effects"]:WaitForChild("Explosion")

-- Sounds
config.whoosh = config.sfxFolder.SlamSounds:WaitForChild("Whoosh")
config.slamImpact = config.sfxFolder.SlamSounds:WaitForChild("GroundSlam")

return config

and your VFXhandler, variables much shorter and neater:

-- VFXHandler.lua
local config = require(game.ReplicatedStorage:WaitForChild("GlobalConfigModule"))

local slamVFX = config.slamVFX
local slamAnim = config.slamAnim
local slamRocks = config.slamRocks
local slamExplo = config.slamExplo
local whoosh = config.whoosh
local slamImpact = config.slamImpact

-- Your VFX handler code here...
2 Likes

Woah I had no clue this was a thing but I had a hunch something like this was possible, I’m gonna have to look into that a bit more and thanks for the pointer to Game Design Support, thought that was more of a art style thing.

It’s alright to use AI to help solve questions too! It serves it’s purpose for resolving syntax issues and discovering things within the engine like this! Just don’t use it to vibe code entire projects though…

1 Like

Oh yea! I was able to even fit everything down into a single line what a great solution!
Screenshot_9

1 Like

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