So my code right now looks like this (For the activation of it) I was wondering if there are any mistakes, better methods I could use.
Also if anyone can help me think of ways to prevent mobs from teleporting when I slow them down (Latency issue).
This will be quite long...
task.spawn(function()
while task.wait(1) do
for _, Mob in CollectionService:GetTagged("ActiveMob") do
if Mob.Parent.Name ~= "MobsInRegion" then continue end
local HumanoidRootPart = Mob:FindFirstChild("HumanoidRootPart")
local Info = Mob:FindFirstChild("Info")
local CurrentTarget = Info and Info:FindFirstChild("CurrentTarget")
if not HumanoidRootPart or not CurrentTarget then continue end
local ClosestPlayer = nil
local ShortestDistance = math.huge
for _, Player in Players:GetPlayers() do
local Character = Player.Character
local PlayerHRP = Character and Character:FindFirstChild("HumanoidRootPart")
if not PlayerHRP then continue end
local Distance = (HumanoidRootPart.Position - PlayerHRP.Position).Magnitude
local Aggro = Distance
if Info.CurrentTarget.Value == Character and Info.FleeOrFight.Value == 2 then Aggro *= 0.3 end
if Aggro > Info.DetectionRange.Value then continue end
if Aggro < ShortestDistance then
ShortestDistance = Distance
ClosestPlayer = Character
end
end
if Info.Aggression.Value == 2 and ClosestPlayer and ClosestPlayer ~= CurrentTarget.Value then
DisconnectTargetUpdates(Mob, Info)
CurrentTarget.Value = ClosestPlayer
local MobDetails = GetMobDetails(Mob)
StartChaseCoroutine(Mob, MobDetails)
elseif not ClosestPlayer and Info.AIState.Value ~= 1 then
DisconnectTargetUpdates(Mob)
CurrentTarget.Value = nil
local MobDetails = GetMobDetails(Mob)
StartWanderCoroutine(Mob, MobDetails)
end
end
end
end)
** You are now Level 23! **