(I hope I got the terminology correct)
So I’ve been working to improve my code for a while now, and I’m currently trying to write more modular code (smaller functions, and modules)
something I recently coded
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local sharedModules = ReplicatedStorage.SharedModules
local serverModules = ServerStorage.ServerModules
local ZoneInformation = require(sharedModules.ZoneInformation)
local ZoneManager = require(serverModules.ZoneManager)
local PlayerZoneManager = {}
local function getPlayerInThisZone(player, zoneName)
return PlayerZoneManager:getZone(zoneName)[player]
end
local function getPlayersZoneName(player)
for zoneName, zone in pairs(ZoneManager.zones) do
local playerInZone = getPlayerInThisZone(player, zoneName)
if playerInZone then
return zoneName
end
end
end
local function setPlayerZone(player, zoneName, value)
local zone = PlayerZoneManager:getZone(zoneName)
if value then
zone[player] = true
ZoneManager:onZonePlayerAdded(player, zoneName)
else
zone[player] = nil
ZoneManager:onZonePlayerRemoved(player, zoneName)
end
end
function PlayerZoneManager:getZone(zoneName)
return ZoneManager.zones[zoneName].players
end
function PlayerZoneManager:addPlayerToZone(player, zoneName)
if not self:getZone(zoneName) then return end
local currentZoneName = getPlayersZoneName(player)
if currentZoneName then
if currentZoneName == zoneName then return end
self:removePlayerFromZone(player, currentZoneName)
end
setPlayerZone(player, zoneName, true)
end
function PlayerZoneManager:removePlayerFromZone(player, zoneName)
local zoneName = zoneName or getPlayersZoneName(player)
setPlayerZone(player, zoneName, false)
end
return PlayerZoneManager
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local sharedModules = ReplicatedStorage.SharedModules
local serverModules = ServerStorage.ServerModules
local ZoneInformation = require(sharedModules.ZoneInformation)
local ZoneManager = {}
ZoneManager.zones = {}
local function init()
for _, zoneInfo in ipairs(ZoneInformation) do
ZoneManager.zones[zoneInfo.name] = {players = {}, enemies = {active = false, enemyObjects = {}}}
end
end
function ZoneManager:onZonePlayerAdded(player, zoneName)
print(player, "added: " .. zoneName)
end
function ZoneManager:onZonePlayerRemoved(player, zoneName)
print(player, "removed: " .. zoneName)
end
init()
return ZoneManager
I’m still working on improving it, but is it bad practice to have regular functions in modules in general?
these regular functions are just for the module and not for any other scripts/modules, should I convert them to methods or is it ok to keep them as is?
my reasoning for making them Non-Methods is because they won’t be used anywhere else but I have no idea if thss is the right way to go about it
also before anyone asks why I have two ZoneManagers when I could use one it’s because I’m also making a enemy zone manager and if I combine enemy + player zone managers it would get confusing to read really quickly