OOP help needed

So I’m kind of new to object oriented programming and I’m trying this library, and I’m trying to make it so that the Ferrari inherits stuff from the NewCar constructor but when I try to print out the price it errors.

function NewCar(name,dealer)
	return {
		
		name = name or "DefaultCar";
		dealer = dealer or "DefaultDealer";
		
		GetName = function(self)
			return self.name
		end;
		
		GetDealer = function(self)
			return self.dealer
		end;
		
	}
end

function NewFerrari(name, dealer,price)
	local ferrari = NewCar(name,dealer)
	
	price = price or 100
	
	ferrari.CheckPrice = function(self)
		return self.price
	end
end

local SportsCar = NewFerrari("Marc's Ferrari", "Marc's Automotive!", 500)
print(SportsCar.CheckPrice())
1 Like

You forgot to return the ferrari

1 Like

Is ferrari an array? 30goddamnchars

I returned price and now it’s outputtin this

1 Like

Well, not really I’m using NewCar as a base constructor to make the Ferrari and Im trying to add my own functions to it, that are just for the Ferrari subclass of newcar

But what is ferrari? A basepart? Array? String? Number?

1 Like

Ferrari is just a variable, but it turns into a class and I assign it the functions of newCar and Im trying to add more functions to it

1 Like

I think your problem is this specific line.

Does it work as intended if you assign the price to ferrari?

ferrari.price = price or 100
1 Like

No thats not the line that is erroring

Because you’re not defining ferrari.price which .CheckPrice is trying to return. Therefore the error occurs in the function because it tries to index a nil value.

I just tried that and its still erroring

Have you tried printing self to see if self returns anything? It should return a table.

But yes, you still need to return ferrari in the end of your function.

Example:

function NewFerrari(name, dealer,price)
	local ferrari = NewCar(name,dealer)
	
	ferrari.price = price or 100
	
	ferrari.CheckPrice = function(self)
		return self.price
	end

	return ferrari
end

yeah I did return ferrari and thats the error it came up with in the output

1 Like

After some testing, I figured what you’re doing wrong. You need to call the function in a different way using : instead of . to get self to work.

Hm I would i implement it like that because I get syntax errors

function NewCar(name,dealer)
	return {
		
		name = name or "DefaultCar";
		dealer = dealer or "DefaultDealer";
		
		GetName = function(self)
			return self.name
		end;
		
		GetDealer = function(self)
			return self.dealer
		end;
		
	}
end

function NewFerrari(name, dealer,price)
	local ferrari = NewCar(name,dealer)
	
	ferrari.price = price or 100
	
	ferrari.CheckPrice = function(self)
		return self.price
	end
	
	return ferrari
end

local SportsCar = NewFerrari("Marc's Ferrari", "Marc's Automotive!", 500)
print(SportsCar:CheckPrice())

This code works perfectly fine for me as long as you didn’t take what I mentioned previously for granted as they also were important notes to get this to work.

2 Likes

Oh its working what did you change exactly because I did the exact same thing

I see you already have your answer solved, but since you’re new to OOP I would recommend reading this for a cleaner way of what you’re doing and for a better understanding of OOP overall

1 Like

All I did was changing the way to fire the .CheckPrice function using :, set the price to ferrari.price and return ferrari at the end of the function.

1 Like

Dang wow how did I not notice that thanks alot man

1 Like