Advertisement
sriyanto

PlayerManager

Oct 11th, 2022
1,167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.39 KB | None | 0 0
  1. local PlayerManager = {}
  2.  
  3. -- Services
  4. local Players = game:GetService("Players")
  5. local ServerStorage = game:GetService("ServerStorage")
  6. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  7.  
  8. -- Modules
  9. local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
  10. local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
  11.  
  12. -- Events
  13. local events = ServerStorage:WaitForChild("Events")
  14. local matchEnd = events:WaitForChild("MatchEnd")
  15.  
  16. -- Variables
  17. local lobbySpawn = workspace.Lobby.StartSpawn
  18. local arenaMap = workspace.Arena
  19. local spawnLocations = arenaMap.SpawnLocations
  20. local playerWeapon = ServerStorage.Weapon
  21.  
  22. -- Values
  23. local displayValues = ReplicatedStorage:WaitForChild("DisplayValues")
  24. local playersLeft = displayValues:WaitForChild("PlayersLeft")
  25.  
  26. local activePlayers = {}
  27.  
  28. local function checkPlayerCount()
  29.     if #activePlayers == 1 then
  30.         matchEnd:Fire(gameSettings.endStates.FoundWinner)
  31.     end
  32. end
  33.  
  34. local function removeActivePlayer(player)
  35.     for playerKey, whichPlayer in pairs(activePlayers) do
  36.         if whichPlayer == player then
  37.             table.remove(activePlayers, playerKey)
  38.             playersLeft.Value = #activePlayers
  39.             checkPlayerCount()
  40.         end
  41.     end
  42. end
  43.  
  44. local function respawnPlayerInLobby(player)
  45.     player.RespawnLocation = lobbySpawn
  46.     player:LoadCharacter()
  47. end
  48.  
  49. local function onPlayerJoin(player)
  50.     player.RespawnLocation = lobbySpawn
  51. end
  52.  
  53. local function preparePlayer(player, whichSpawn)
  54.     player.RespawnLocation = whichSpawn
  55.     player:LoadCharacter()
  56.    
  57.     local character = player.Character or player.CharacterAdded:Wait()
  58.     -- Give the player a tool
  59.     local sword = playerWeapon:Clone()
  60.     sword.Parent = character
  61.  
  62.     local humanoid = character:WaitForChild("Humanoid")
  63.    
  64.     humanoid.Died:Connect(function()
  65.         respawnPlayerInLobby(player)
  66.         removeActivePlayer(player)
  67.     end)
  68. end
  69.  
  70. function PlayerManager.sendPlayersToMatch()
  71.     local availableSpawnPoints = spawnLocations:GetChildren()
  72.    
  73.     for playerKey, whichPlayer in pairs(Players:GetPlayers()) do
  74.         table.insert(activePlayers,whichPlayer)
  75.  
  76.         -- Gets a spawn location and then removes it from the table so the next player gets the next spawn
  77.         local spawnLocation = availableSpawnPoints[1]
  78.         table.remove(availableSpawnPoints, 1)
  79.         preparePlayer(whichPlayer, spawnLocation)
  80.     end
  81.  
  82.     playersLeft.Value = #activePlayers
  83. end
  84.  
  85. game.Players.PlayerAdded:Connect(onPlayerJoin)
  86.  
  87. return PlayerManager
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement