Hi, I followed a youtube tutorial by yellow mustang to make an enemy handler using coroutines as I don’t understand coroutines at all and thought that it would be helpful to make the script and study it for better understanding. The problem is, it doesn’t work! I’ve tried checking youtube and devforum topics but I really wasn’t able to figure it out. I’m not used to not having my errors thrown at me for me to fix so I’m very confused. I looked it up and apparently you can use coroutine.resume() to find errors or something?? I’m really confused and in over my head.
All i’m looking for is some simple pointers on how to easily tell which of these is erroring. As long as I can figure that out, I can learn by myself what i’ve done wrong and how to fix it.
Thank you for taking the time to read!
local collectionService = game:GetService("CollectionService")
local thieves = {}
function spawner(func,...)
local co = coroutine.wrap(func)
co(...)
end
function checkDist (part1, part2)
if typeof(part1) ~= Vector3 then part1 = part1.Position end
if typeof(part2) ~= Vector3 then part2 = part2.Position end
return (part1 - part2).Magnitude
end
function updateTarget()
local humans = collectionService:GetTagged("Human")
for _, thief in pairs(thieves) do
local target = nil
local dist = 200
for _, human in pairs(humans) do
local root = human.RootPart
if root and human.Health > 0 and checkDist(root,thief.root) < dist and human.Parent.Name ~= thief.char.Name then
dist = checkDist(root, thief.root)
target = root
end
end
thief.target = target
end
end
spawner(function()
while wait(1) do
updateTarget()
end
end)
function pathToTarget(thief)
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(thief.root.Position, thief.target.Position)
local waypoints = path:GetWaypoints()
local currentTarget = thief.target
for i, v in pairs(waypoints) do
if v.Action == Enum.PathWaypointAction.Jump then
thief.human.Jump = true
else
thief.Human:MoveTo(v.Position)
spawner(function()
wait(0.5)
if thief.human.WalkToPoint.Y > thief.root.Position.Y then
thief.human.Jump = true
end
end)
thief.human.MoveToFinished:Wait()
if not thief.target then
break
elseif checkDist(currentTarget,waypoints[#waypoints]) > 10 or currentTarget ~= thief.target then
pathToTarget(thief)
break
end
end
end
end
function moveHandler(thief)
while wait(1) do
if thief.human.Health <= 0 then
break
end
if thief.target then
pathToTarget(thief)
end
end
end
function attack(thief)
local human = thief.target.Parent.Humanoid
human:TakeDamage(math.random(15,25))
end
spawner(function()
while wait(0.5) do
for _, thief in pairs(thieves) do
if thief.target then
if checkDist(thief.target, thief.root) < 0 then
attack(thief)
end
end
end
end
end)
function tagHuman(instance)
local humanoid = instance:FindFirstChildWhichIsA("Humanoid")
if humanoid then
collectionService:AddTag(humanoid, "Human")
end
end
function removeThief(thief)
local index = table.find(thieves, thief)
table.remove(thieves, index)
wait(5)
thief.char:Destroy()
thief.clone.parent = workspace
end
function addThief(thiefHumanoid)
table.insert(thieves, {
char = thiefHumanoid.Parent,
root = thiefHumanoid.RootPart,
human = thiefHumanoid,
target = nil,
clone = thiefHumanoid.Parent:Clone(),
})
for _, thief in pairs(thieves) do
if thief.Human == thiefHumanoid then
thief.human.Died:Connect(function() removeThief(thief) end)
for i, v in pairs(thief.char:GetDescendants()) do
if v:IsA("BasePart") and v:CanSetNetworkOwnership() then
v:SetNetworkOwner(nil)
end
end
spawner(moveHandler, thief)
break
end
end
end
workspace.ChildAdded:Connect(tagHuman)
collectionService:GetInstanceAddedSignal("Thief"):Connect(function(thiefHumanoid)
addThief(thiefHumanoid)
end)
function initialize()
for _, v in pairs(collectionService:GetTagged("Thief")) do
local found = false
for _, x in pairs(thieves) do
if x.human == v then
found = true
end
end
if not found then
addThief(v)
end
end
end
initialize()