I need help with this module script

Ok so im working on a small side project of a tds type game and i came along a problem of making the mobs destroy after finishing the path.
module script:

local SererStorage = game:GetService("ServerStorage")
local mob = {}

function mob.Move(mop, map)
	local humanoid = mop:WaitForChild("Humanoid")
	local waypoints = map.WayPoints

	for waypoint=1, #waypoints:GetChildren() do
		humanoid:MoveTo(waypoints[waypoint].Position)
		humanoid.MoveToFinished:Wait()
	end
	
	mob:Destroy()
end

function mob.Spawn(name, quantity, map)
	local mobExists = SererStorage.Mobs:FindFirstChild(name)
	if mobExists then
		for i=1, quantity do
			task.wait(0.5)
			local newMob = mobExists:Clone()
			newMob.HumanoidRootPart.CFrame = map.Start.CFrame
			newMob.Parent = workspace
			coroutine.wrap(mob.Move)(newMob, map)
			
		end
	
	else
		warn("Requested mob does not exist")
	end

Output:


thx in advance

Easiest thing I can see to fix this quickly would be to use the same thing as your parameter, because mob:Destroy would be destroying the module table

So change it to this

function mob.Move(mop, map)
	local humanoid = mop:WaitForChild("Humanoid")
	local waypoints = map.WayPoints

	for waypoint=1, #waypoints:GetChildren() do
		humanoid:MoveTo(waypoints[waypoint].Position)
		humanoid.MoveToFinished:Wait()
	end
	
	mop:Destroy()
end

I found out the mistake, I typed mop instead of mob
So here the correct script now :smiley:


local SererStorage = game:GetService("ServerStorage")
local mob = {}

function mob.Move(mob, map)
	local humanoid = mob:WaitForChild("Humanoid")
	local waypoints = map.WayPoints

	for waypoint=1, #waypoints:GetChildren() do
		humanoid:MoveTo(waypoints[waypoint].Position)
		humanoid.MoveToFinished:Wait()
	end
	
	mob:Destroy()
end

function mob.Spawn(name, quantity, map)
	local mobExists = SererStorage.Mobs:FindFirstChild(name)
	if mobExists then
		for i=1, quantity do
			task.wait(0.5)
			local newMob = mobExists:Clone()
			newMob.HumanoidRootPart.CFrame = map.Start.CFrame
			newMob.Parent = workspace
			coroutine.wrap(mob.Move)(newMob, map)
			
		end
	
	else
		warn("Requested mob does not exist")
	end

Also I’m going to mark @Stephenesta_aqui solution because somehow looking at his script made me recognize my mistake

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