Advertisement
Quoteory

Better Player Zoning

Nov 19th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1. local ServerStorage = game:GetService("ServerStorage")
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3. local sharedModules = ReplicatedStorage.SharedModules
  4. local serverModules = ServerStorage.ServerModules
  5. local ZoneInformation = require(sharedModules.ZoneInformation)
  6.  
  7.  
  8. local PlayerZoneManager = {}
  9. local zones = {}
  10.  
  11. local function start()
  12.     for _, zoneInfo in ipairs(ZoneInformation) do
  13.         zones[zoneInfo.name] = {}
  14.     end
  15. end
  16.  
  17. local function getZone(zoneName)
  18.     return zones[zoneName]
  19. end
  20.  
  21. local function getPlayerZoneByName(player, zoneName)
  22.     return getZone(zoneName)[player]   
  23. end
  24.  
  25. local function findPlayerZone(player)
  26.     for zoneName, zone in pairs(zones) do
  27.         local playerInZone = getPlayerZoneByName(player, zoneName)
  28.         if playerInZone then
  29.             return zoneName
  30.         end
  31.     end
  32. end
  33.  
  34. local function setPlayerZone(player, zoneName, value)
  35.     local zone = getZone(zoneName)
  36.     if value then
  37.         zone[player] = true
  38.         PlayerZoneManager:onZonePlayerAdded(player, zoneName)
  39.     else
  40.         zone[player] = nil
  41.         PlayerZoneManager:onZonePlayerRemoved(player, zoneName)
  42.     end
  43. end
  44.  
  45.  
  46. function PlayerZoneManager:onZonePlayerAdded(player, zoneName)
  47.     print(player, "added: " .. zoneName)
  48. end
  49.  
  50. function PlayerZoneManager:onZonePlayerRemoved(player, zoneName)
  51.     print(player, "removed: " .. zoneName)
  52. end
  53.  
  54.  
  55. function PlayerZoneManager:addPlayerToZone(player, zoneName)
  56.     if not getZone(zoneName) or getPlayerZoneByName(player, zoneName) then return end
  57.     self:findAndRemovePlayerZone(player)
  58.     setPlayerZone(player, zoneName, true)
  59. end
  60.  
  61. function PlayerZoneManager:removePlayerFromZone(player, zoneName)
  62.     local zone = getZone(zoneName)
  63.     if getPlayerZoneByName(player, zoneName) then
  64.         setPlayerZone(player, zoneName, false)
  65.     end
  66. end
  67.  
  68. function PlayerZoneManager:findAndRemovePlayerZone(player)
  69.     local zoneName = findPlayerZone(player)
  70.     if zoneName then
  71.         self:removePlayerFromZone(player, zoneName)
  72.     end
  73. end
  74.  
  75. start()
  76.  
  77. return PlayerZoneManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement