How to Organize and make scripting easier?

Hello!

Is there a way that I could easily script without having to scroll all the way down just to change one thing.

I want to somehow make my script shorter and easier to update but I don’t know how I would do it.

Here are some images related to what I’m trying to say:

Screenshot_121

The script is very long and it makes it harder to add an event everytime I add new ones, I want to somehow organize these events into classes and somehow spawn them there.

I want to organize the script so its easier to update, I don’t know how I would do it though…

1 Like
  1. Try making functions
  2. Making variables
  3. Make sure its readable
  4. Use Format Selection
  5. I think thats it.
  6. Update : Do not forget to use “--” so you can see your comments while scrolling down.

You can tabularize that long conditional chain.

This forum is old but I stumbled upon it, personally Id store the data inside a table and check if the “event” is equal to an array. For an example,

  local table = {
      [1] = {
          Name = "",
          Yaxis = 0,
      },
      [2] = {
          Name = "",
          Yaxis= 0,
      },
     -- keep going
  }
  
  -- check 
  for i,v in pairs(table) do
      if v == event then
          local clone = game.ServerStorage.Events[v.Name]:Clone()
          local postion = Vector3.new(PositionX, v.Yaxis, PositionZ)
          local cf = position
  
          clone:PivotTo(cf)
          clone.Parent = workspace.EventFolder
      end
  end
local ServerStorage = game:GetService("ServerStorage")

local EventStatus = ReplicatedStorage.Values.EventStatus
local Prompt = script.Parent.ProximityPrompt

EventStatus.Value = "Waiting on Button to be pressed..."

local EventFolder = workspace:WaitForChild("EventFolder")

local EVENTS = {
	[1]  = {Name = "Police", Height = 4.5},
	[2]  = {Name = "Marine", Height = 3},
	[3]  = {Name = "Zombie", Height = 2.691},
	[4]  = {Name = "Helicopter", Height = 9.659},
	[15] = {Name = "Truss", Height = 6, Timed = true},
	[16] = {Name = "Cloud", Height = 45.8, Timed = true},
	[17] = {Name = "TallTower", Height = 153.6, Timed = true},
	[18] = {Name = "GrassTower", Height = 6, Timed = true},
}

local X_RANGE = {-523, 17}
local Z_RANGE = {-437, 388}

local function spawnEvent(eventId)
	local data = EVENTS[eventId]
	if not data then return end

	local template = ServerStorage.Events:FindFirstChild(data.Name)
	if not template then
		warn("Missing event:", data.Name)
		return
	end

	local object = template:Clone()

	local x = math.random(X_RANGE[1], X_RANGE[2])
	local z = math.random(Z_RANGE[1], Z_RANGE[2])

	local position = Vector3.new(x, data.Height, z)

	object.Parent = EventFolder

	if object:IsA("Model") then
		object:PivotTo(CFrame.new(position))
	else
		object.Position = position
	end

	-- Auto destroy timed events
	if data.Timed then
		local timer = math.random(50, 200)
		task.delay(timer, function()
			if object then
				object:Destroy()
			end
		end)
	end
end

Prompt.Triggered:Connect(function(player)
	local eventId = math.random(1, 31)
	spawnEvent(eventId)
end)

instead of doing repeating if statements use a use a configuration table its more scalable.

Return Repeated Logic into one function.