local item = {}
item.__index = item
function item.new(color)
local data = setmetatable({}, item) --this just makes it so that if the thing you're trying to access doesn't appear in data, it will search 'item' for it
item.color = color
return data
end
function item.ChangeColor(passedItem, color)
passedItem.color = color
end
So in this example, you could call item.ChangeColor()
local myItem = item.new(myItem, Color3.new(1,1,1))
myItem.ChangeColor(myItem, Color3.new(0,0,0)
Basically you have to pass in myItem so that the code can actually adjust the data for that object. Otherwise it would be confused because color isn’t in that scope.
It’s annoying to have to say myItem every time though. You are already using the myItem variable in that line so it feels a bit odd to be passing it in as well.
So without changing any of the above code, the colon will automatically pass the item you called it on as the first argument. So these 2 things do the same
myItem.ChangeColor(myItem, Color3.new(0,0,0)) --You have to pass myItem in
myItem:ChangeColor(Color3.new(0,0,0) --The colon automatically passes the myItem in as the first item
Now this works when you make the function using ‘.’ syntax.
function item.ChangeColor(passedItem, color)
passedItem.color = color
end
But what if you use colon syntax for making the function?
function item:ChangeColor(color)
self.color = color
end
With colon syntax you notice I removed the passedItem parameter. That’s because when making a function with colon it secretly adds a parameter which it calls self. You can’t see it, but it’s there right before the color parameter.
To see it a bit more clearly you can call it 2 ways again
The colon method
myItem:ChangeColor(Color3.new(0,0,0)
Or, you could call it with dot method
myItem.ChangeColor(myItem, Color3.new(0,0,0)) --You have to pass myItem in
Colon automtically added the self variable, but the dot method you would again have to pass in myItem which ends up being the self variable.