function startFight(creep, enemyThrone, targetCreepList) local distanceToMiddle =…
- User
function startFight(creep, enemyThrone, targetCreepList) local distanceToMiddle = (middle - creep.PrimaryPart.Position).Magnitude
codelocal targetCreep while targetCreep == nil and #targetCreepList > 0 do targetCreep = targetCreepList[math.random(1, #targetCreepList)] if targetCreep.Humanoid.Health <= 0 then targetCreep = nil end end if targetCreep ~= nil then local initialPos = middle while creep.Humanoid.Health > 0 and targetCreep.Humanoid.Health > 0 do print(11) -- Проверка расстояния между крипами local distance = (targetCreep.PrimaryPart.Position - creep.PrimaryPart.Position).Magnitude if distance <= creepsattackrange + 2 then print(11) coroutine.wrap(function() local cd = false if cd == false then cd = true targetCreep.Humanoid:TakeDamage(math.random(0,5)) wait(0.5) cd = false end end)() creep.Humanoid:MoveTo(targetCreep.PrimaryPart.Position) creep.Humanoid.MoveToFinished:Wait() else -- Если враждебный крип убежал далеко, двигаться к нему local path = pathfindingService:CreatePath() path:ComputeAsync(creep.PrimaryPart.Position, targetCreep.PrimaryPart.Position) local waypoints = path:GetWaypoints() for i = 1, #waypoints do creep.Humanoid:MoveTo(waypoints[i].Position) creep.Humanoid.MoveToFinished:Wait() -- Проверка на расстояние после каждого подхода к враждебному крипу distance = (targetCreep.PrimaryPart.Position - creep.PrimaryPart.Position).Magnitude if distance <= creepsattackrange then return end -- Проверка на удаление от координаты “middle” local distanceToMiddle = (middle - creep.PrimaryPart.Position).Magnitude if distanceToMiddle > 40 then creep.Humanoid:MoveTo(middle) -- Возвращаемся к позиции “middle” creep.Humanoid.MoveToFinished:Wait() return end end end end -- Возвращаемся к начальной позиции перед завершением creep.Humanoid:MoveTo(initialPos) creep.Humanoid.MoveToFinished:Wait() else -- Если не найдена цель для атаки из списка creep.Humanoid:MoveTo(enemyThrone.Position) -- Двигаться к вражескому трону creep.Humanoid.MoveToFinished:Wait() end - Assistant
The error "ServerScriptService.Script:14: attempt to get length of an Instance value" occurs because you are trying to get the length of a value that is not a table or string. In this case, the variable "targetCreepList" is expected to be a table, but it seems to be an Instance instead.
To fix this error, make sure that the "targetCreepList" variable is correctly passed as a table to the "startFight" function. You can check if it is a valid table using the "type" function:
luafunction startFight(creep, enemyThrone, targetCreepList) if type(targetCreepList) ~= "table" then return -- or handle the error in an appropriate way end -- rest of the code endBy including this check, you can prevent the error from occurring and handle the situation gracefully when the "targetCreepList" is not a table.