Problem with oop class

I was making a tycoon game to learn more aout oop programming but I’ve got a issue, I wanted to see if the tycoon has already a owner, but the problem is that all the tycoons are “connected” so if one tycoon has a owner the script will interpretate that as if all the tycoons have already a owner, here’s the module script:

--Define the class Tycoon
local Tycoon = {}
Tycoon.__index = Tycoon

--Constructor of the class Tycoon
function Tycoon.create()
	self = setmetatable({},Tycoon)
	return self
end

--Analise if the tycoon has a owner 
function Tycoon.new(player,Tycoon)
	if self.Owner == nil then
		self.Owner = player
		self.Name = Tycoon
		print(self.Owner.Name .. " is owner of " .. self.Name)
	else
		print(self.Name .. " is alread taken by " ..self.Owner.Name )
	end
end

return Tycoon

The main script

Tycoon = require(game:GetService("ServerScriptService").Tycoon)
part = script.Parent

Tycoon.create()

part.Touched:Connect(function(hit)
	if hit.Parent:IsA("Model") then
		local char = hit.Parent
		local Player = game:GetService("Players"):GetPlayerFromCharacter(char)
		local InitialTycoon = Tycoon.new(Player,"Initial")
	end
end)

I already tried to pass a table as a argument in the Tycoon.create() function and the only idea i have to fix it is to make a Module Script for every tycoon but that is not very efficient.

1 Like

Your Tycoon.create function returns the object, you must save it in a variable for this to be useful

local Initial = Tycoon.create()

To use self in a method you must define the function with a colon like so

function Tycoon:new(player)

Now using the method with our object the same way will yield different results for each object.

part.Touched:Connect(function(hit)
	if hit.Parent:IsA("Model") then
		local char = hit.Parent
		local Player = game:GetService("Players"):GetPlayerFromCharacter(char)
		local InitialTycoon = Initial:new(Player) -- using method
	end
end)

It may also be helpful to look into the CollectionService so you don’t have to copy this script into each tycoon object. But I hope this has helped you with OOP in lua, it is kind of weird compared to other languages!

2 Likes

It worked thank you, but let me see if I’m getting this straight, with the “Initial = Tycoon.create” i’m defining Initial as a variable for table that I created but what’s the difference between a point and a colon? ty one more time.

The reference to self, when the function uses a dot it does not reference the self.

function Tycoon.new()
--here self is nil
end
function Tycoon:new()
--self is Tycoon:{}
end

Edit: Although it is essentially the same as doing

Tycoon.new(Tycoon)
1 Like

Thanks bro, so when I use a colon the function will take the word before the colon as a parameter, while with the dot the function will take the table tycoon has a parameter, right?

If by this you mean that the function will only take the table tycoon as parameter if you willingly give it as a parameter then yes, all of that is true.

The reason why you would use self and colons is because when you create a new “Tycoon” object, by using self you are referencing that newly created tycoon object’s table, instead of the original template table it comes from.

1 Like

Okay, so I understand now, ty!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.