Help with Object Oriented Programming

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am really new to OOP, just discovered it today and I wanted to try and rescript some stuff to make my life easier. This code in particular is being used to script an NPC.

(This is probably just a huge beginner mistake, since I’m feeling like I’m missing something extremely obvious here.)

  1. What is the issue? Include screenshots / videos if possible!

Whenever I call :Move(), the script errors saying:

attempt to call missing method ‘Move’ of table

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried looking for other posts to see if theyve had the same issue but it seems that this is a small enough one that it just doesn’t get documented that frequently?
I also tried to double check and see if my format matched other people’s working code and I still came up empty.

-- Module Script

Npc = {}
Npc._index = Npc

function Npc.new(Goal, ColorFromRGB)
end

function Npc:Move(PositionInVector3)
	local MoveValue = self:FindFirstChild("MovePosition")
	MoveValue.Value = PositionInVector3
end

return Npc

-- Server Script

Npc = require(game.ReplicatedStorage.NpcModule)

local TempThing = Npc.new("red", Color3.fromRGB(219, 127, 127))
Npc.new("blue", Color3.fromRGB(128, 187, 219))

TempThing:Move(workspace.RedGoal.Position) -- this is the line that errors 

The code using the oop seems ok.

But the constructor has some things missing.

Compare between yours and one from a tutorial:

1 Like

Thank you for the help! I had the set meta table stuff hidden in Npc.new, but I just realized I used one instead of two underscores in __index! A typo like that would’ve drove me crazy trying to find, thanks again for putting them side by side!