Attempt to call nil value?

I decided to try tackling OOP, and I set up an interactive example following the advice given by tutorials on the devforum. However when I try calling a function within the ‘newPhone’ object, it keeps on saying it is nil, even though the function exists. What am I doing wrong?
Server Script

local phoneOOP = require(game.ServerStorage.phoneOOP)
local newPhone  = phoneOOP.New("RandomPhone", "RandomModel", 70)
setmetatable(newPhone, phoneOOP)
newPhone:Charge() --Erroring Here
print(newPhone.Battery)

Module

local phoneTable = {}
phoneTable._index = phoneTable 
function phoneTable.New(Brand, Model, Battery)
	local newPhone = {}
	setmetatable(newPhone, phoneTable)
	newPhone.Brand = Brand
	newPhone.Model = Model
	newPhone.Battery = Battery
	return newPhone
end

function phoneTable:Charge()
	self.Battery += 10
end
return phoneTable

Thanks!

The phone is probably not loaded yet try using a wait

Ok I’ll try that. Give me one second

Just try using a :WaitForChild() to wait for the phone!

I think this has to have 2 underscores from all the OOP examples I’ve seen

phoneTable.__index = phoneTable

And as @Afrxzo mentioned, you’re using setmetatable twice when you already do it in the .new

1 Like

You don’t need to set ‘newPhones’ metatable again, since you have already set it here,

1 Like

It’s a table, not an actual instance

Yes, it does. _index does nothing, __index does something.
All metamethods use double underscores, @JeffTheEpicRobloxian

1 Like

Thanks for the quick response guys! Really appreciate the support. It’s working now

1 Like

Glad that we have helped you out! If you have anymore issues don’t be afraid to make another post!

1 Like