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…
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.