Advertisement
sriyanto

MatchManager

Oct 11th, 2022
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.60 KB | None | 0 0
  1. local MatchManager = {}
  2.  
  3. -- Services
  4. local ServerStorage = game:GetService("ServerStorage")
  5. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  6.  
  7. -- Module Scripts
  8. local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
  9. local playerManager = require(moduleScripts:WaitForChild("PlayerManager"))
  10. local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
  11. local timer = require(moduleScripts:WaitForChild("Timer"))
  12.  
  13. -- Events
  14. local events = ServerStorage:WaitForChild("Events")
  15. local matchStart = events:WaitForChild("MatchStart")
  16. local matchEnd = events:WaitForChild("MatchEnd")
  17.  
  18. -- Values
  19. local displayValues = ReplicatedStorage:WaitForChild("DisplayValues")
  20. local timeLeft = displayValues:WaitForChild("TimeLeft")
  21.  
  22. -- Creates a new timer object to be used to keep track of match time.
  23. local myTimer = timer.new()
  24.  
  25. -- Local Functions
  26. local function stopTimer()
  27.     myTimer:stop()
  28. end
  29.  
  30. local function timeUp()
  31.     matchEnd:Fire(gameSettings.endStates.TimerUp)
  32. end
  33.  
  34. local function startTimer()
  35.     print("Timer started")
  36.     myTimer:start(gameSettings.matchDuration)
  37.     myTimer.finished:Connect(timeUp)   
  38.  
  39.     while myTimer:isRunning() do
  40.         -- Adding +1 makes sure the timer display ends at 1 instead of 0.
  41.         timeLeft.Value = (math.floor(myTimer:getTimeLeft() + 1))
  42.         -- By not setting the time for wait, it offers more accurate looping  
  43.         wait()
  44.     end
  45.  
  46. end
  47.  
  48. -- Module Functions
  49. function MatchManager.prepareGame()
  50.     playerManager.sendPlayersToMatch()
  51.     matchStart:Fire()
  52. end
  53.  
  54. matchStart.Event:Connect(startTimer)
  55. matchEnd.Event:Connect(stopTimer)
  56.  
  57. return MatchManager
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement