Need help on understanding metatables

Recently, I decided to dive deeper into Object-Oriented Programming (OOP) because my code wasn’t readable, and I was interested in learning how module scripts and metatables work. So, I decided to create something myself, and this is how it turned out.

ModuleScript

donut = {}
donut.__index = donut


function donut.new(model,position,parent)
	local newDonut = {}
	setmetatable(newDonut,donut)
	
	newDonut.Model = model
	newDonut.Position = position
	newDonut.Parent = parent
	
	
	return newDonut
end

function donut:Enlarge()
	self.Scale = self.Scale + 50
end



return donut


ServerScript

local donutModule = require(game.ReplicatedStorage.ModuleScript)

donut = donutModule.new(game.ReplicatedStorage.Donut,Vector3.new(-64.843, 3.281, 251.054),workspace)

donut:Enlarge()


However, I didn’t see the donut or the enlargement; instead, I had to face an error.

Appreciate any help or tips!

You didn’t define newDonut.Scale in donut.new()

do newDonut.Scale = 0 in the .new method

Thank you, everyone. The error is fixed, but I still don’t see the donut. Can you tell me why this happened even though I’m running this in a normal script?

Set the parent of the model in the .new method like model.Parent = parent

Thank you; that actually worked.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.