OOP; ServerScriptService.Script:3: attempt to index nil with 'Drink'

Hi!
I’m getting the error in the title.
Script:
Module

local Potion = {}
Potion.__index = Potion

function Potion.newHealthPotion(parent)
	local newHealthPotion = {}
	setmetatable(newHealthPotion, Potion)
	newHealthPotion.Heal = 50
end

function Potion:Drink(humanoid)
	--Animation play 
	print(self.Heal)
	humanoid.Health += self.Heal
end


return Potion

Server

local PotionSystem = require(script.Parent.ModuleScript)
local newPotion = PotionSystem.newHealthPotion(workspace)
newPotion:Drink()

What’s wrong with it? I just wanted it to print self.Heal

1 Like

I may be wrong but dont you have to setmetatable before doing __index?

edit: nvm. i see what you did there

1 Like

Figured it out. Forgot to return newHealthPotion at the end of Potion.newHealthPotion

1 Like

I’m pretty sure you don’t have to, if you just use __index on a regular table it won’t error, it won’t do anything.

ye, I thought you were gonna set a metatable to your potions table. i didnt realise potions was gonna be the metatable.

1 Like