Advice on keeping scripts organized

Hey! I recently started my first game project but there has to be something I’m doing wrong. I’ve been writing most of my code because I don’t exactly know how to pass values into other scripts. For example there are local variables in one script, that I can’t use in other scripts so I just have a bunch of if statements and for loops in one entire script.

How can I keep my scripts more organized and use other scripts to do so?

Here is an example of my code…
I would have setting a players role, team, the intermission gui, rolegui, in the same script.

			debounce = true
			print(chosen.Name.. " is the DRAWER!")
			chosen.Team = Teams.Drawer
			
			for i, plr in ipairs(Players:GetChildren()) do
				
				if plr ~= chosen then

					plr.Team = Teams.Guessers
					print(plr.Name.. " , you are a GUESSER!")
					--delay(5, function()
						plr:WaitForChild("PlayerGui"):WaitForChild("RoleGui").RoleFrame.Visible = true
						plr:WaitForChild("PlayerGui"):WaitForChild("RoleGui").RoleFrame.DisplayRole.Text = "Guesser"
						plr:WaitForChild("PlayerGui"):WaitForChild("RoleGui").RoleFrame.DisplayRole.TextColor3 = Color3.fromRGB(43,121,125)
					--end)
					delay(15, function()
						plr:WaitForChild("PlayerGui"):WaitForChild("RoleGui").RoleFrame.Visible = false
					end)
				end
			end
			
			for i = 15, 1, -1 do
				wait(1)
				CurrentStatus.Value = "Intermission: "..i.. " seconds remaining"
				Value = CurrentStatus.Value
				RemoteEventNotInRound:FireAllClients(Value)
				
				if i == 15 and chosen then
					chosen:WaitForChild("PlayerGui"):WaitForChild("RoleGui").RoleFrame.Visible = true
					chosen:WaitForChild("PlayerGui"):WaitForChild("RoleGui").RoleFrame.DisplayRole.Text = "Drawer"
					chosen:WaitForChild("PlayerGui"):WaitForChild("RoleGui").RoleFrame.DisplayRole.TextColor3 = Color3.fromRGB(43,121,125)
					
				end
				
				if i == 2 then
					InRound = true
					print("Round Started")
					chosen:WaitForChild("PlayerGui"):WaitForChild("RoleGui").RoleFrame.Visible = false
					
					if chosen then
						local Brush = BrushTool:Clone()
						Brush.Parent = chosen.Humanoid.Parent
					end
					
					while PlayersInServer < 2 do
						task.wait()
						NotEnoughPlayers()
					end
					
					while PlayersInServer >= 2 and InRound == true do
						for i = 15, 1, -1 do
							wait(1)
							CurrentStatus.Value = i.. " seconds remaining"
							Value = CurrentStatus.Value
							RemoteEventInRound:FireAllClients(Value)
							
							if i == 1 then 
								InRound = false
								debounce = false
								print("Round Ended")
								
								for i, playerss in ipairs(Players:GetChildren()) do
									playerss.Team = Teams.Neutral
								end
								
								while PlayersInServer < 2 do
									task.wait()
									NotEnoughPlayers()
								end
							end
						end
					end
				end
			end
		end
	end)  
end)```

I would Recommend Having ModuleScripts so your Scripts are more Organized, and to not repeat yourself, as well with having Variables For Specific Objects to make your Script a bit more clean

1 Like

I would very much recommend looking into module scripts! They are a game changer to making code more readable and maintainable.

They also allow you to do OOP (Object Oriented Programming), which is great for doing advanced stuff in an easy way.

First of all use variables instead of calling :WaitForChild() for instances. E.g.

local RoleFrame = chosen:WaitForChild("PlayerGui"):WaitForChild("RoleGui").RoleFrame
RoleFrame.Visible = true
RoleFrame.DisplayRole.Text = "Drawer

instead of:

chosen:WaitForChild("PlayerGui"):WaitForChild("RoleGui").RoleFrame.Visible = true
chosen:WaitForChild("PlayerGui"):WaitForChild("RoleGui").RoleFrame.DisplayRole.Text = "Drawer"

etc.

Also, as others have mentioned, use ModuleScripts to separate sections of code.

You could also research single-script architecture to further organise your game with module scripts (but this may be more advanced to learn)

1 Like

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