Hey I recently learned about OOP and I wanted to try it out with mobs. I made a mob class and it works but I’m just not sure if it’s effective or good.
local Mob = {}
Mob.__index = Mob
function Mob.new(enemy,position,damage)
local self = {}
setmetatable(self,Mob)
self.enemy = enemy
self.position = position
self.damage = damage
self.enemy.Parent = workspace
self.enemy:MoveTo(position)
self:Patrol(self.enemy.HumanoidRootPart,30)
return self
end
function Mob:Patrol(origin,range)
local isDead = false
local spotTarget = false
local part = Instance.new("Part",workspace)
part.Size = Vector3.new(range,range + 10,range)
part.Transparency = .75
part.CFrame = origin.CFrame
part.Anchored = true
part.CanCollide = false
self.enemy.Humanoid.Died:Connect(function()
isDead = true
part:Destroy()
wait(2)
self.enemy:Destroy()
end)
local atk = false
self.enemy.HumanoidRootPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if hit.Parent.Name == self.enemy.Name then return end
if isDead then return end
if atk then return end
atk = true
hit.Parent.Humanoid:TakeDamage(self.damage)
wait(2)
atk = false
end
end)
local touch = false
local plr
part.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) and hit.Name == "HumanoidRootPart" then
touch = true
if plr == hit.Parent then
return
else
plr = hit.Parent
end
print(plr)
spawn(function()
while touch do
if isDead then return end
wait()
self.enemy.Humanoid:MoveTo(hit.Parent.HumanoidRootPart.Position)
end
end)
end
end)
part.TouchEnded:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) and hit.Name == "HumanoidRootPart" then
if hit.Parent ~= plr then return end
touch = false
plr = nil
spawn(function()
while not touch do
if isDead then return end
wait()
self.enemy.Humanoid:MoveTo(self.position)
end
end)
end
end)
end
return Mob