Object Oriented Programming - Set part properties

Heya DevForum. So, as a rather new programmer I’m just learning stuff.
While I’m scripting my game and integrating OOP in it, I came across something that I don’t really understand, or know how it works.
Essentially, this is what I have:

local Target = {}
Target.__index = Target

function Target.new(target: BasePart, targetNumber: number)
	local self = setmetatable({}, Target)

	self.target = target
	self.targetNumber = targetNumber

	return self
end

function Target:TargetHit(newPos: Vector3)
	if not self.target then
        -- Handle whatever error
		return
	end
	self.Position = newPos
end

So here, when I call the method :TargetHit(), it does nothing (However it does work, because I used a print statement. It just does not change the position, and there are no errors either.):

local part = Target.new(workspace.Part, 1)
part:TargetHit(Vector3.new(0,10,0))

It does work when I change its position like normal:

workspace.Part.Position = Vector3.new(0,10,0)

So I’m only assuming I’m doing this wrong. Can someone tell me what I’m supposed to do? I don’t have vast OOP knowledge. Thanks in advance.

Ah yeah, couldn’t find a topic like this in the forum.

image
Have you tried printing self.target and stuff cuz my first guess is it doesnt have target and just ends the function

You used self.Position and not self.target.Position

1 Like

Jesus, am I seriously this stupid? I made a whole topic when all I was missing was one word.

Confirmed, I am an idiot.

It’s ok, we all make mistakes.
Good luck in your future… scripting career I guess.

Yeah, now the only thing I’m mad at is that it didn’t even error. Should say Position does not exist within a table. Don’t know why.

It was because you were setting a NEW position variable inside the table. Thats why it didnt error.
Basically, the table looked like this:

{
target = somePart,
targetNumber = someNumber,
Position = somePosition
}

That sets a new variable? Ah, right, I forgot how tables worked. I need proper sleep next time, thanks lol.

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