Hey,
I have an unrevised round module for a game I’m working on and I’m lowkey interested on how other developers would revise this code. You can see some easy improvements can be done here, but I want to see your approach
--!nocheck
-- services
local Players, ReplicatedStorage, ServerScriptService = game:GetService("Players"), game:GetService("ReplicatedStorage"), game:GetService("ServerScriptService")
-- folders
local Packages, ServerPackages, Source, Modules, Events, Bosses= ReplicatedStorage.Packages, ServerScriptService.Packages, ReplicatedStorage.Source, ServerScriptService.Source.Modules, ReplicatedStorage.Source.Shared.Events, ReplicatedStorage.Source.Shared.Bosses
-- requires
local Knit, Maid, Data, Config, Replica, Shrink, RNG = require(Packages.Knit), require(Packages.Maid), require(ServerPackages.Data), require(Source.Shared.Config), require(Modules.ReplicaServer), require(script.Shrink), require(script.RNG)
-- variables
local Intermission, Participants, Constants, Paths = Config.Settings.Intermission, Config.Participants, Config.Constants, Config.Paths
-- tables
local GameService, Maid = Knit.CreateService{Name = "GameService"}, Maid.new()
local GameReplica = Replica.New({
Token = Replica.Token("Game"),
Data = {
Round = 0,
Text = "",
TypewriteText = "",
Event = "",
Began = false,
Shrink = false,
Replenish = false,
},
})
Constants.GAME_REPLICA = GameReplica -- for easy access among other server scripts
-- functions
local function getRandomEvent(round: number)
local randomEvent, randomDifficulty = math.random(1, #Events:GetChildren()), math.random(1, #Constants.DIFFICULTIES)
local event, difficulty = Events:GetChildren()[randomEvent]:: ModuleScript, Constants.DIFFICULTIES[randomDifficulty]
if round >= 25 then
difficulty = "Hard"
end
-- boss
if RNG.Roll() == "Boss" then
local randomBoss = math.random(1, #Bosses:GetChildren())
event = Bosses:GetChildren()[randomBoss]:: ModuleScript
difficulty = "Boss"
end
if RNG.Roll() == "Merge" and round < 25 then
local events = {}
for i = 1, 2 do
local module = Events:GetChildren()[math.random(1, #Events:GetChildren())]
local event = require(module)
if (type(event["Merge"]) == "boolean") then
if #events ~= 0 and module.Name == events[1].Name then -- same event
repeat
module = Events:GetChildren()[math.random(1, #Events:GetChildren())]
event = require(module)
until (type(event["Merge"]) == "boolean") and module.Name ~= events[1].Name
end
table.insert(events, module)
else -- if its not mergeable; repeat until mergeable
repeat
module = Events:GetChildren()[math.random(1, #Events:GetChildren())]
event = require(module)
until (type(event["Merge"]) == "boolean") and if #events ~= 0 then module.Name ~= events[1].Name else true
table.insert(events, module)
end
end
GameReplica:Set({"Event"}, `{tostring(events[1])} {tostring(events[2])} Merge`)
return events[1], events[2], difficulty
else
GameReplica:Set({"Event"}, `{tostring(event)} {difficulty}`)
end
return event, difficulty
end
local function isGameOver(): boolean?
if #Participants ~= 0 then return end
-- Player dies, Event is over -> gets called twice;
-- Began is only back to true when intermission is finished.
if not GameReplica.Data["Began"] then return true end -- return true to avoid firing new event
GameService.Clean()
GameReplica:Set({"Began"}, false)
task.wait(Constants.GAME_END_DELAY)
if Constants.CURRENT_REQUIRE
and (type(Constants.CURRENT_REQUIRE["Clean"]) == "function") then
Constants.CURRENT_REQUIRE.Clean() -- cleans event if there is a clean func
end
if Constants.CURRENT_REQUIRE_2
and (type(Constants.CURRENT_REQUIRE_2["Clean"]) == "function") then
Constants.CURRENT_REQUIRE_2.Clean() -- cleans event if there is a clean func
end
Shrink.Return()
GameService.RoundAsync()
return true
end
local function shrinkCircle(boolean: boolean?)
if boolean then -- shrink
GameReplica:Set({"Shrink"}, true)
GameReplica:Set({"Replenish"}, false)
GameReplica:Set({"TypewriteText"}, Constants.SHRINK_TEXT)
task.wait(Constants.CIRCLE_DELAY)
Shrink.Shrink(Config.Settings.ShrinkPercentage)
task.wait(Constants.CIRCLE_DELAY)
end
if not boolean then -- return
GameReplica:Set({"Shrink"}, false)
GameReplica:Set({"Replenish"}, true)
GameReplica:Set({"TypewriteText"}, Constants.REPLENISH_TEXT)
task.wait(Constants.CIRCLE_DELAY)
Shrink.Return()
task.wait(Constants.CIRCLE_DELAY)
end
end
function GameService.RoundAsync()
local Round = 0-- initialize round / default round
GameReplica:Set({"Round"}, 0)
for Seconds = Intermission, 1, -1 do -- intermission
GameReplica:Set({"Text"}, Constants.INTERMISSION_TEXT:format(Seconds))
task.wait(1)
end
for _, Participant in Players:GetPlayers() do
if not Participant.Character then continue end -- don't enter player if no character.
local Humanoid = Participant.Character.Humanoid :: Humanoid
if Humanoid.Health <= 0 then continue end -- don't enter player if dead
table.insert(Participants, Participant)
Maid:GiveTask(Humanoid.Died:Connect(function()
table.remove(Participants, table.find(Participants, Participant))
local HighscoreStore: Data.Store = Data.GetStore(Participant, "Highscore")
local Highscore = HighscoreStore:Get()
if Highscore < Round then -- set highscore if > round
HighscoreStore:Set(Round)
end
if isGameOver() then return end -- end game
end))
Maid:GiveTask(task.spawn(function()
task.wait(Constants.TELEPORT_DELAY)
-- teleport players
if not Participant.Character -- character not found // if intermission <= 5
or not Participant.Character:FindFirstChild("HumanoidRootPart") then
table.remove(Participants, table.find(Participants, Participant))
if isGameOver() then return end -- end game
else
local root = Participant.Character.HumanoidRootPart
root.CFrame = Paths.Map.Spawn.CFrame + Vector3.new(0, 5, 0)
end
end))
Maid:GiveTask(Players.PlayerRemoving:Connect(function(player: Player)
if not table.find(Participants, player) then return end
local FindNew = if
table.find(Participants, player) == 1
or table.find(Players:GetPlayers(), player) == 1
then true else false
table.remove(Participants, table.find(Participants, player))
if isGameOver() then return end -- end game
if FindNew then
GameReplica:FireClient(Participants[1]) -- get a new client holder
end
end))
end
GameReplica:Set({"Text"}, "")
GameReplica:Set({"Began"}, true)
GameReplica:FireClient(Participants[1]) -- starting client holder
Constants.EVENT_CONNECTION = GameReplica.OnServerEvent:Connect(function(_, UUID: string)
if UUID == Constants.CHOOSE_UUID then -- chooses event
Constants.EVENT, Constants.DIFFICULTY, Constants.MERGE = getRandomEvent(Round)
end
if UUID == Constants.EVENT_UUID then -- starts event
if Constants.DIFFICULTY == "Hard" or Constants.MERGE then
Round += 3
elseif Constants.DIFFICULTY == "Boss" then
Round += 20
else
Round += 1
end
if Constants.DIFFICULTY ~= "Boss" then
if Round >= 25 then
-- force shrink above 25 rounds
if Shrink.isCircleTooSmall() then
shrinkCircle(false)
else
shrinkCircle(true)
end
else
if Round % 10 == 0 then shrinkCircle() end-- replenish circle every 10 rounds
if Round % 3 == 0
and Round % 10 ~= 0
and not Shrink.isCircleTooSmall() then shrinkCircle(true) end
end
else
Shrink.Return()
end
GameReplica:Set({"Round"}, Round)
if Constants.MERGE then
task.spawn(function()
Constants.CURRENT_REQUIRE_2 = require(Constants.DIFFICULTY)
Constants.CURRENT_REQUIRE_2.StartAsync() -- will be easy
end)
end
Constants.CURRENT_REQUIRE = require(Constants.EVENT)
Constants.CURRENT_REQUIRE.StartAsync(Constants.DIFFICULTY)
if Constants.CURRENT_REQUIRE_2 -- cleans second require if there is one
and (type(Constants.CURRENT_REQUIRE_2["Clean"]) == "function") then
Constants.CURRENT_REQUIRE_2.Clean() -- cleans event if there is a clean func
Constants.MERGE = nil
end
if isGameOver() then return end -- end game
Constants.EVENT, Constants.DIFFICULTY = getRandomEvent(Round)
end
end)
end
function GameService.KnitInit()
GameReplica:Replicate()
GameService.RoundAsync()
end
function GameService.Clean()
Maid:DoCleaning()
if Constants.EVENT_CONNECTION then Constants.EVENT_CONNECTION:Disconnect() end
end
return GameService