Is there a way to improve this mob script I made?

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
1 Like

Depends, you ought to test out how effective it is yourself by putting it to work in a game. If you’re considered with the efficiency or performance of the script or you’re unsatisfied with it, please point out specifically where you are unsatisfied and why that’s the case. Supplying your aims would also be helpful: for example, are you gunning for performance or something else?

Sorry for being blunt on my post and not posting enough details :sweat_smile:. For my script I was more concerned about the performance because I use spawn functions, and I’ve heard that they are not very efficient to use. Do you think spawn functions are a good method for this type of stuff?

Spawn still has a minimum guarantee of running at least one thread so feel free, but no, your use of spawn wouldn’t be appropriate in this case. I can’t fully grasp what any of the Touched portions are doing but you have no debounces in any of them and as such, you will be spawning up a large number of while loops which will tank performance for you or lead to unexpected behaviours.

Mind explaining what that part of code does? There might be a better way of going about that.

Yeah sure! So basically in the Mob:Patrol function, I make a part around the mob. If a player then touches the part, or basically goes inside that part (which is supposed to act like the mob’s range) the mob then moves towards the player that entered the part. If the player then stops touching the part then mob returns back to it’s original position in the middle of the part. I hope I made sense :sweat_smile:

That made more sense, yeah. In that case, you’ll probably want to change the functionality there. Rather than relying on a part and physics (Touched is a physics-based event), you can have the Mob search for players around it. If a player is near the mob, then it can move towards that player. If the player moves out of range or the mob moves out of range of its original position, then it can stop following the player and return. Rinse and repeat with a couple of combinations.

1 Like

Ok thank you I’ll try that out.