Unable to use self throughout module script even after returning it

So I have a pathfinding script that works perfectly except for one thing that bothers me. I have initialized self keyword in my OOP.new function and returned it, but when I attempt to use self in other functions, it says “unknown global self”.
Here’s are pieces of my script as an example;
The initializing function:

function PaOOP.new(bot)
	local self = setmetatable({}, PaOOP)
	self.bot = bot
	return self
end

And one function where I need to use the self keyword

function PaOOP.TargetPlayer(self)
	local path = pathfindingService:CreatePath()
	local fwaypoints = workspace.Waypoints:GetChildren()
	local player = PaOOP.GetNearestPlayer(self.bot.HumanoidRootPart.Position)
	if player ~= nil then
		path:ComputeAsync(self.bot.HumanoidRootPart.Position,player.Character.HumanoidRootPart.Position)
		if path.Status == Enum.PathStatus.Success then
			local waypoints = path:GetWaypoints()
			for i, w in pairs(waypoints) do
				self.bot.Humanoid:MoveTo(w.Position)
			end
		else
			self.bot.Humanoid:MoveTo(workspace.SpawnLocation.Position)
		end
	else
		self.bot.Humanoid:MoveTo(fwaypoints[math.random(1,#fwaypoints)].Position)
		self.bot.Humanoid.MoveToFinished:Wait()
	end
end

To fix this, I put self as a parameter to these functions, but I’m sure there is a better method.

You need to use " : " instead of " . " if you want to pass self into your functions

function PaOOP.TargetPlayer(self) --Nope, baddd
function PaOOP:TargetPlayer() --Very good my friend (no need to put self in the function parameters)

Ohhh thanks, because I was using for first time only and I wasn’t very experienced on this

Np! It gets more and more easier with time dw

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