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.