"Connecting" a model or instance to OOP Module script

I don’t know how to say it, but I want to somehow connect my zombie model with the module script. I don’t know how to do that. Here is my script:

Zombie = {}
Zombie._index = Zombie

function Zombie.new(position,damage,speed,health,color)
	newZombie = {}
	setmetatable(newZombie,Zombie)
	
	
	newZombie.Position = position
	newZombie.Damage = damage
	newZombie.Speed = speed
	newZombie.Health = health
	newZombie.Color = color
	
	return newZombie
end

function Zombie:FindTarget(target)
	
end

function Zombie:Respawn()
	if self.Health == 0 then
		local ZombieClone = game.ReplicatedStorage.Zombie:Clone()
		
	end
end

function Zombie:MoveToTarget()
	
end

So would I just say “Zombie = game.Workspace.Zombie” or something else?

1 Like

There’s a few different ways you could do something like what you’ve described. Mainly, the route you take depends on how you intend to use the ModuleScript.

If you intend to have a ModuleScript located inside each Zombie model, then you could set up something like this:

ModuleScript as an Object
local ZombieAI = {
   Alive = false --Bit of an oxymoron here, but in this sense Alive means whether or not the Run function has been called
   --You can also include more properties as you would like
}

ZombieAI.Run = function(self)
   --This function might include some sort of loop which calls other functions in order to make the Zombie do its Zombie things
end

ZombieAI.Destroy = function(self)
   --This function would cleanup the Zombie's model and disconnect any events
end

return ZombieAI

Otherwise if you would like to have an overarching class for the Zombie (Better option if you intend to have many Zombies), then you might set it up something like this:

ModuleScript as a Class
local Zombie = {
   --This module is serving as a class for creating Zombies. It is not itself a Zombie
   --To get a new Zombie (object), call this modules Zombie.new() function.
}

Zombie.new = function(...)
   return createZombie(...)
end

function createZombie(args) --Can change this to include parameters for a newly created Zombie
   --First, make a new table to serve as the organizational structure for a SINGLE Zombie
   local newZombie = {
      Alive = false,
      Model = nil,
   }
   --Create a new Zombie model for this newZombie object
   newZombie.Model = createZombieModel()
   --Next, set up any methods you'd like to include. Again, these methods will be specific to one Zombie
   newZombie.Spawn = function(self, spawnAt)
      if not self.Alive then
         self.Alive = true
         self.Model.Parent = workspace
         self.Model:SetPrimaryPartCFrame(spawnAt)
      end
   end

   newZombie.Destroy = function(self)
      if self.Alive then
         self.Alive = false
         if self.Model then
            self.Model:Destroy()
         end
      end

   return newZombie
end

function createZombieModel()
   local zombieModel = Instance.new("Model")
   local center = Instance.new("Part", zombieModel)

   zombieModel.PrimaryPart = center
   return zombieModel
end

return Zombie

Obviously, those were pretty plain examples that don’t actually do anything yet. I’ll let you fill in those details. Also, there may be a typo or two. Didn’t write or test any of the code in Studio.

Goodluck!

2 Likes

Ahhh Thanks for the reply! I will look more into this.

Also do I gotta index anything. Like for example do I do, “Zombie._index = Zombie”?

In my experience the implementation of Lua’s metatable (including __index and such) features is often more work than its worth