Confused on OOP

I just started learning OOP and I decided I wanted to try and implement it into my game, what I want to do here have a couple of player owned robots, store their info, and put them in a table so that later the module could grab the info and do whatever based on what the player tells the robot to do, but I have four problems.

Here’s what I got so far in a module script:

local RobotInfo = {
	Common = {
		Info = {
			Damage = 1,
		},
		"Joe",
		"Jane",
	},
}

local Robot = {}
Robot.__index = Robot

Robot.Robots = {}

function Robot:NewRobot()
	local self = setmetatable({}, Robot) 
	local info
	
	for _,v in pairs (RobotInfo) do
		if v[self.Name] then
			info = v["Info"]
		end
	end

	self.Damage = info.Damage -- Attempt to index nil with "Damage"
	self.Battery = 100
	self.BatteryStrength = 1
	self.Status = "Idle"

	coroutine.wrap(function()
		while wait(1) do
			if self.Status ~= "Dead" then
				if self.Battery <= 1 then
					self.Battery -= 1
					wait(self.BatteryStrength-1)
				else
					self.Status = "Dead"
				end
			end
		end
	end)()
    	
    return self
end

function Robot.Activate(Entity)
	print(Entity)
	table.insert(Robots, Robot.NewRobot) -- Not sure if this is right
end

The first problem is that info is nil.

The second problem is that even if info wasn’t nil, I don’t know if if I’m storing the info properly, there aren’t any tutorials I’ve found on how to store this kind of stuff.

The third problem even if info wasn’t nil and I stored it properly, I don’t how to grab this info to use on the robot associated with it later in the module (ex. Robot’s battery dies → robot dies).

The four problem is that I don’t know how to script the module so that it would make the robot do something based on what the player wants.

Can someone help me?

1 Like

self doesn’t contain a Name property. I don’t know where exactly you expect this to come from. If you want to create a Robot by passing in a certain name you have to provide that as an argument in your NewRobot constructor.

E.g.

function Robot:NewRobot(name)
  local self = setmetatable({}, Robot)
  local info

  self.Name = name -- for example if you pass in Robot:NewRobot('Jane')

  for _, v in pairs(RobotInfo) do
    if (v[self.Name]) then
      info = v.Info
      break
    end
  end

  self.Damage = info.Damage
  self.Battery = 100
  self.BatteryStrength = 1
  self.Status = 'Idle'

  ... -- just insert your other code here
end

function Robot.Activate(Entity)
  print(Entity)
  table.insert(Robot.Robots, Robot:NewRobot('Joe')) -- not entirely sure what Entity is so I just put Joe here
end

You can create methods for your Robot by adding methods to your Robot table since you set that as the index of the metatable. E.g.

function Robot:SayHello()
  print('Hello my name is ' .. self.Name)
end
2 Likes

Ah, didn’t know self was only the object and nothing else, thanks for that info!

After doing this, how would I then go to the folder in an choose a bot that I could manipulate using the module or another script?

I’m a bit confused what folder? Is the robot an actual in-game instance?

Yep, wanted to attach the info to the in game robot that spawns so that I don’t have to use string values and int values as much as I did before

Also the folder is this:

local Robot = {}
Robot.__index = Robot

Robot.Robots = {} <-- This one

function Robot:NewRobot()
	local self = setmetatable({

I see. Also that’s a table not a folder. A folder is a Roblox instance and a table is a Lua type. There’s a big difference that’s why I was confused. If you’re returning self then you have the Robot object. You just have to insure you return it to wherever you want to use it.

Sorry I get things mixed up a lot, but also thanks for the info!

1 Like

No problem. If you have have any other questions feel free to ask.

But wait, what if the thing that I’m returning it to is a table in which the robot was inserted in? Since I put the robot as an index and it’s info as the value in the table from the module Since I table inserted the robot + it’s info, how would I then reference it in order to use on another function within the module?