#Make my round system repeat
25 messages · Page 1 of 1 (latest)
hi
so
it's a normal script and a module script
wait
how do I make a code thing
so its organized
** You are now Level 20! **
-- Modules
local Round = require(script.Round)
-- Services
local PlrS = game:GetService("Players")
local RepS = game:GetService("ReplicatedStorage")
-- Folders
local Events = RepS:WaitForChild("Events")
local Remotes = Events:WaitForChild("Remotes")
-- Events
local RoundEvent = Remotes:WaitForChild("Round-Event")
-- Settings
local Round_Time = 20
local Inter_Time = 10
local MinPlayers = 2
local MurdS_Time = 10
-- Tables
local Players_Ready = {}
-- Flag to track if a round is active
local RoundActive = false
local RoundFinished = true -- Track if the previous round has finished
-- Functions
function Start()
if not RoundFinished then
print("Round is still in progress. Please wait until it's finished.")
return
end
if RoundActive then
print("Round is already active!") -- Prevent starting a new round if one is already active
return
end
-- Set the flag to indicate the round is active
RoundFinished = false
RoundActive = true
-- Start intermission countdown
for i = Inter_Time, 0, -1 do
if #Players_Ready < MinPlayers then return end -- Not enough players, don't start round
task.wait(1)
print(i)
-- Notify players about intermission time
for _, player in PlrS:GetPlayers() do
if player then
RoundEvent:FireClient(player, `Intermission: {i} sec left`)
end
end
if i == 0 then
-- Notify players that the game is starting
for _, player in PlrS:GetPlayers() do
if player then
RoundEvent:FireClient(player, `Game Starting...`)
end
end
-- Start the round (calls Round.Init)
Round.Init(Round_Time, Players_Ready, MurdS_Time)
-- Set the RoundActive flag to true to prevent overlapping rounds
RoundActive = true
end
end
end
function Manage_Player(Player, State)
if State == "Join" then
if not table.find(Players_Ready, Player.Name) then
table.insert(Players_Ready, Player.Name)
end
-- Start the round if enough players are ready and no round is active
if #Players_Ready >= MinPlayers and not RoundActive and RoundFinished then
Start()
end
elseif State == "Left" then
if table.find(Players_Ready, Player.Name) then
local index = table.find(Players_Ready, Player.Name)
table.remove(Players_Ready, index)
end
end
end
-- Automatically repeat the round when conditions are met
function RepeatRound()
while true do
task.wait(2) -- Small delay before checking to restart the round
-- Ensure round is not active and previous round is finished
if #Players_Ready >= MinPlayers and not RoundActive and RoundFinished then
Start() -- Restart the round if conditions are met
end
end
end
-- Starters
RoundEvent.OnServerEvent:Connect(function(...)
Manage_Player(...)
end)
game.Players.PlayerRemoving:Connect(function(...)
Manage_Player(..., "Left")
end)
game.Players.PlayerAdded:Connect(function(player)
Manage_Player(player, "Join")
end)
-- Start the automatic round repeat process
RepeatRound()
Main script
-- constants
local IsActive = false
-- Services
local RepS = game:GetService("ReplicatedStorage")
-- Spawns
local NormalSpawn = workspace:WaitForChild("normalspawn")
local OthersSpawn = workspace:WaitForChild("otherspawn")
local LobbySpawn = workspace:WaitForChild("LobbySpawn")
-- Folders
local Events = RepS:WaitForChild("Events")
local Remotes = Events:WaitForChild("Remotes")
local MainGun = RepS:WaitForChild("main gun")
-- Weapon
local ShotGun = MainGun:WaitForChild("Shotgun")
-- Events
local RoundEvent = Remotes:WaitForChild("Round-Event")
local Round = {}
Round.__index = Round
function Round.Init(Round_Time, Players_Ready, MurdS_Time)
setmetatable({
Round_Time = Round_Time,
Players_Ready = Players_Ready,
MurdS_Time = MurdS_Time,
Murderer = nil
}, Round):Ready()
end
-- Functions
function Round:Ready()
-- Prevent starting the round if one is already active
if IsActive then
print("Round is already active!")
return
end
-- Randomly select the murderer
local Random_Murd = self.Players_Ready[math.random(1, #self.Players_Ready)]
if Random_Murd then
self.Murderer = game.Players[Random_Murd]
self:Start()
end
end
function Round:Start()
IsActive = true
-- Spawn innocent players
for _, playerName in pairs(self.Players_Ready) do
local plr = game.Players[playerName]
if plr and plr.Name ~= self.Murderer.Name then
local HumRP = plr.Character:WaitForChild("HumanoidRootPart")
HumRP.CFrame = NormalSpawn.CFrame
end
end
-- Spawn murderer after timer
for i = self.MurdS_Time, 0, -1 do
task.wait(1)
RoundEvent:FireClient(self.Murderer, `Murderer spawning in: {i}`)
if i == 0 then
self.Murderer.Character:WaitForChild("HumanoidRootPart").CFrame = OthersSpawn.CFrame
local new_ShotGun = ShotGun:Clone()
new_ShotGun.Parent = self.Murderer.Backpack
for _, playerName in pairs(self.Players_Ready) do
local plr = game.Players[playerName]
if plr then
RoundEvent:FireClient(plr, `Murderer spawned!`)
end
end
end
end
-- Round timer countdown
for i = self.Round_Time, 0, -1 do
task.wait(1)
for _, playerName in pairs(self.Players_Ready) do
local plr = game.Players[playerName]
if plr then
RoundEvent:FireClient(plr, `Time Left: {i}`)
end
end
-- Check if murderer died
self.Murderer.Character:WaitForChild("Humanoid").Died:Connect(function()
for _, playerName in pairs(self.Players_Ready) do
local plr = game.Players[playerName]
if plr then
RoundEvent:FireClient(plr, `Murderer Died!`)
end
end
self:End()
end)
-- End round when time runs out
if i == 0 then
self:End()
for _, playerName in pairs(self.Players_Ready) do
local plr = game.Players[playerName]
if plr then
RoundEvent:FireClient(plr, `Game Ended`)
end
end
end
end
end
function Round:End()
-- Kill all players at the end of the round
for _, playerName in pairs(self.Players_Ready) do
local plr = game.Players[playerName]
if plr then
local HumRP = plr.Character:WaitForChild("HumanoidRootPart")
HumRP.CFrame = LobbySpawn.CFrame
plr.Character:BreakJoints() -- Make all players die
end
end
-- Reset round variables
IsActive = false
table.clear(self.Players_Ready) -- Clear the players list
table.clear(self, nil)
end
function Round:IfActive()
return IsActive
end
return Round
Module Script
I cant quite figure out how to make it repeat
is the repeatround function not firing start?
it is
i aint readn allat
lol
@ocean storm can u help me after
instead of a while loop checking every 2 seconds, i would fire the start function 30 seconds after the game ends.
I'm not too experienced of a scripter, can you make the changes and send me the updated version of the script?
did you write the module?
No, my friend did
figures, this is a simple design error anyone with metatable knowledge can fix