Basically I’m learning OOP and I’m creating a zombie enemy system to learn it I’ve created a contructer function and am working on it’s pathfinding however The zombie will not pathfind and I’m getting no errors.
I have done a lot of debugging and there is one thing I’d like to point out, The pathfinding script works if I just put it in a Seperate rig and change some stuff so that it does not use OOP.
ZombieModuleCode:
local Zombie = {}
Zombie.__index = Zombie
function Zombie.new(position)
local self = setmetatable({}, Zombie)
self.Model = game.ServerStorage.ZombieModel:Clone()
self.Model.Name = "Zombie"
self.Model.HumanoidRootPart.Anchored = false
self.Model.Parent = game.Workspace
self.Model.HumanoidRootPart.Position = Vector3.new(position)
self.health = 100
self.speed = 8
self.damage = 10
self.state = "Idle"
print(self.Model)
return self
end
function Zombie:Update()
local PathfindingService = game:GetService("PathfindingService")
--What to do depending on the Zombies state
if self.state == "Idle" then
local ReconPoints = game.Workspace.ReconPoints:GetChildren()
local MaxDistance = 200
local ReconTable = {}
--Get all ReconPoints near it and choose one to randomly go to
for _, ReconPoint in pairs(ReconPoints) do
local DistanceFromReconPoint = (ReconPoint.Position - self.Model.HumanoidRootPart.Position).Magnitude
print("DistanceFromReconPoint is" .. DistanceFromReconPoint )
if DistanceFromReconPoint < MaxDistance then
table.insert(ReconTable, ReconPoint)
end
end
print(ReconTable)
local ReconPointPickedNum = math.random(1, #ReconTable)
local ReconPointPicked = ReconTable[ReconPointPickedNum]
local path = PathfindingService:CreatePath()
path:ComputeAsync(self.Model.HumanoidRootPart.Position, ReconPointPicked.Position)
--If path is possible then go to each waypoint only start gong to the next one when your 5 studs away from current one
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(path:GetWaypoints()) do
self.Model.Humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
self.Model.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
local goal = false
while goal == false do
task.wait(0.1)
local GoalDistance = (self.Model.HumanoidRootPart.Position - waypoint.Position).Magnitude
print(GoalDistance)
if GoalDistance < 5 then
goal = true
end
end
end
else
print("Path not computed")
end
elseif self.state == "Chasing" then
-- Implement chasing behavior
elseif self.state == "Attacking" then
-- Implement attacking behavior
end
print("Not Something")
end
return Zombie
Code for testing constructer function and pathfinding function:
local Zombie = require(game.ReplicatedStorage.Zombie)
local ZombiePosition = game.Workspace.ZombieSpawner.Position
NewZombie = Zombie:new(ZombiePosition)
while true do
NewZombie:Update()
task.wait(5)
end