Hi devs, i finally got access to the forum. Im making a bomb game using OOP
Ok so, i have two server scripts, one Main and other Match, and some Modules, the constructor called “Bomb” and then the classes (like Void or Missile). now i have a _G table where i have the current players, so my modules, Main and Match can access the players that are currently playing the game (also tell me if im doing OOP wrong )
-- example using my bomb module
local Bomb = {}
Bomb.__index = Bomb
function Bomb.New(model, pos, damage , radius, Time)
local newBomb = setmetatable({}, Bomb)
newBomb.Model = model
newBomb.Model.CFrame = pos
newBomb.DetonationTime = Time
newBomb.damage = damage
newBomb.radius = radius
return newBomb
end
function Bomb:GetNearestPlayers()
local model = self.Model
local Targets = {}
for _, player in pairs(_G.Match.CurrentPlayers) do
-- magnitude checks and stuff, i may use spatial queries later, dunno
table.insert(Targets, player)
end
return Targets
end
return Bomb
-- one of the class module (just in case)
local Bomb = require(ServerStorage.Modules.Bomb)
local Boom = {}
Boom.__index = Boom
setmetatable(Boom, Bomb)
function Boom.New(...)
local self = setmetatable(Bomb.New(...), Boom)
return self
end
function Boom:__Init()
local Targets = self:GetNearestPlayers()
for _, player in pairs(Targets) do
local Char = player.Character
if Char then
Char.Humanoid:TakeDamage(self.damage)
end
end
end
return Boom
-- Main script spawns the bombs and so
-- match...well, basically inserts players in _G.Match.CurrentPlayers and if a player dies, then remove it from the table
Now, i know using _G is a really bad practice, but also i know Bindables do exist, but i heard Modules are better, but the problem is, how? I understand that modules are local (attached to the script that required it), so i dont know how, example in case I didn’t explain well:
-- module
local module = {}
local Number = 1
function module.AddNumber()
Number += 1
end
function module.GetNumber()
return Number
end
return module
-- script 1
local module = require(path)
task.wait()
module.AddNumber()
print(module.GetNumber()) -- prints 2
--script 2
local module = require(path)
task.wait(1)
print(module.GetNumber()) -- prints 1
So i dont get it, why people says not to use _G and instead use Modules to share data, if theres the local thing? I saw devs saying that also modules can be used instead of Bindables to communicate between script1 and script2, so what do i do to share the CurrentPlayers table and if a player dies, then remove it from the table, but also want “Main” or “Bomb” to know that the player isnt there anymore. (Sorry if my english is bad)