I’m making a simple ordering system that uses an “Order” class module and it has functions like :AddItem() and :RemoveItem(). I believe my code should work as intended, but it keeps throwing an error. Here is the code for both the Order Module and LocalScript:
Module:
local Order = {}
Order.__index = Order
function Order.new()
local self = setmetatable({}, Order)
self.Items = {}
return self
end
function Order:AddItem(item)
table.insert(self.Items, item)
end
function Order:RemoveItem(item)
table.remove(self.Items, table.find(self.Items, item))
end
return Order
LocalScript:
local Order = require(game:GetService("ReplicatedStorage").Order)
local currentOrder = nil
currentOrder = Order.new()
currentOrder:AddItem("Burger")
print(currentOrder.Items)
I just tested your code in a test place and it worked fine. Just from a read through, it looks like it should work anyway.
It might be to do with initialising currentOrder as nil, that’s the only thing I can think of. Are you using this object anywhere else in the script?
setmetatable will return the first parameter passed to it after the metatable has been set, your code and the code the OP used produce the exact same effect.
Yeah, I need to be able to access the order everywhere in the script hence why it’s defined at the start and then updated throughout. Still really don’t know why it’s working for everyone else but myself.