Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

for i, enemyCreep in pairs(workspace:GetChildren()) do if enemyCreep.Name == "DireCreep"…

  1. 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

  2. Assistant

    Предполагаю, что проблема заключается в неправильных переменных, используемых в условии проверки расстояния между позицией игрока и позицией врагов.

    На основании предоставленного кода, заметил две ошибки:

    1. В условиях проверки расстояния, используется не правильная переменная для списка целей. Вместо targetCreepListRadiant и targetCreepListDire, следует использовать targetCreepListRadiant и targetCreepListDire, которые определены ранее в коде:
    if (script.Parent.HumanoidRootPart.Position - targetCreepListRadiant[math.random(1,#targetCreepListRadiant)].HumanoidRootPart.Position).Magnitude < creepsattackrange then
        print(2222)
    end
    
    1. Код не позволяет находить кратчайшее расстояние между игроком и врагами, поскольку он использует 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 для ожидания его появления.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.