Currently I’m making a game which is heavily based on rounds, similar to how Epic Mini Games works (game isn’t related to that).
Anyways I am completely aware that the way I made the round system is pretty bad & I’ve been thinking of ways to improve it.
To begin off, I have a script in ServerScriptService:
local ss = game.ServerStorage
local rp = game.ReplicatedStorage
local settings_ = require(script.Settings) --module of settings to be easily able to change stuff that might be imbalanced
function fireStatus(text)
game.ReplicatedStorage.RoundStatus:FireAllClients(text) --text appears on player's screen about game info
end
function startRound()
local randomizer = ss.ScriptReplications:GetChildren()
local pickedGamemode = randomizer[math.random(1,#randomizer)]:Clone()
pickedGamemode.Parent = game.ServerScriptService
pickedGamemode.Disabled = false
end
function getAliveAndNotAfkPlayers() --returns a table full of players who have their hp above 0 and dont have afk mode on
local t = {}
for _,v in pairs(game.Players:GetChildren()) do
if v.Afk.Value == false and v.Character.Humanoid.Health > 0 then
table.insert(t,v)
end
end
return t
end
rp.RoundStarted.CANCEL.Changed:Connect(function()
wait(.1)
for _,v in pairs(script.Parent:GetChildren()) do
if v.Disabled then
game.Debris:AddItem(v,2) --this is only here to prevent scripts from duplicating & taking up memory
break
end
end
end)
spawn(function()
while wait(settings_["StartTime"]) do
if not rp.RoundStarted.Value and #game.Players:GetChildren() > 1 and #getAliveAndNotAfkPlayers() > 1 then
wait(5)
startRound()
end
end
end)
rp.RoundStarted.Timer.Changed:Connect(function()
if rp.RoundStarted.Timer.Value <= 0 then
for _,v in pairs(game.Players:GetChildren()) do
if v.inGame.Value then
v.Character.Humanoid:TakeDamage(100)
end
end
end
end)
tl;dr: script clones a script from between specific modes & will not proceed until the round triggered the cancel bool to true and have the script deleted
For a bit of reference, these are how some stuff are set up:
These are the scripts in ServerStorage where a game mode will be chosen from.
These are the bools in ReplicatedStorage
what Timer does if it hits 0 the round automatically gets cancelled no matter what part it’s at, basically a force reset
What I’ve been thinking of improving:
- Replacing the script cloning with using
require()
on module scripts. - Replacing
spawn()
with coroutines
So back to why I’m even posting this, is there any ways to improve my code?
Also if you don’t understand anything about it, please let me know and I’ll explain what it does.