for i, enemyCreep in pairs(workspace:GetChildren()) do if enemyCreep.Name == "DireCreep"…
- User
for i, enemyCreep in pairs(workspace:GetChildren()) do if enemyCreep.Name == "DireCreep" then table.insert(targetCreepListRadiant, enemyCreep) coroutine.wrap(function() while wait() do if (script.Parent.HumanoidRootPart.Position - targetCreepListDire[math.random(1,#targetCreepListDire)].HumanoidRootPart.Position).Magnitude < creepsattackrange then print(1111) end end end)() elseif enemyCreep.Name == "RadiantCreep" then table.insert(targetCreepListDire, enemyCreep) coroutine.wrap(function() while wait() do if (script.Parent.HumanoidRootPart.Position - targetCreepListRadiant[math.random(1,#targetCreepListRadiant)].HumanoidRootPart.Position).Magnitude < creepsattackrange then print(2222) end end end)() end end почему print срабатывает на любом расстоянии? Roblox studio lua
- Assistant
Предполагаю, что проблема заключается в неправильных переменных, используемых в условии проверки расстояния между позицией игрока и позицией врагов.
На основании предоставленного кода, заметил две ошибки:
- В условиях проверки расстояния, используется не правильная переменная для списка целей. Вместо
targetCreepListRadiantиtargetCreepListDire, следует использоватьtargetCreepListRadiantиtargetCreepListDire, которые определены ранее в коде:
if (script.Parent.HumanoidRootPart.Position - targetCreepListRadiant[math.random(1,#targetCreepListRadiant)].HumanoidRootPart.Position).Magnitude < creepsattackrange then print(2222) end- Код не позволяет находить кратчайшее расстояние между игроком и врагами, поскольку он использует
math.randomдля выбора случайного врага из списка. Вместо этого, рекомендуется использовать функциюFindNearestTorso, чтобы найти ближайший вражеский Torso к игроку:
local function FindNearestTorso(position) local minDistance = math.huge local nearestTorso = nil for i, enemy in ipairs(targetCreepListRadiant) do local distance = (position - enemy:WaitForChild("HumanoidRootPart").Position).Magnitude if distance < minDistance then minDistance = distance nearestTorso = enemy:WaitForChild("HumanoidRootPart") end end return nearestTorso end coroutine.wrap(function() while wait() do local nearestTorso = FindNearestTorso(script.Parent.HumanoidRootPart.Position) if nearestTorso and (script.Parent.HumanoidRootPart.Position - nearestTorso.Position).Magnitude < creepsattackrange then print(2222) end end end)()Обратите внимание, что вы также должны убедиться, что у вражеских
enemyCreepобъектов есть дочерний элемент"HumanoidRootPart"и вы можете использоватьWaitForChildдля ожидания его появления. - В условиях проверки расстояния, используется не правильная переменная для списка целей. Вместо
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.