错误:Humanoid 不是模型的有效成员

问题描述 投票:0回答:1

我正在制作一款塔防游戏,塔楼与敌人作战。塔楼有射程,如果他们发现敌人靠近其射程,就会攻击它。我的问题是我的塔开始正常杀死敌人,然后收到此错误消息“Humanoid 不是有效成员”。

寻找最近敌人的塔脚本

local Tower = script.Parent
local enemies = game.Workspace.enemies
local range = 20
local target= nil
local Animator = Tower.Humanoid.Animator
local Attack = Animator:LoadAnimation(Tower.AttackAnim)

     function nearestenemy()
         for i, enemy in ipairs(enemies:GetChildren()) do
             local distance = (Tower.HumanoidRootPart.Position - enemy.HumanoidRootPart.Position).Magnitude
        
             if (distance < range) and (enemy.Humanoid.Health > 0) then
                  target = enemy
                 range = distance
             end 
         end
    
         range = 20
    
          if target then
             return target.Humanoid
         else
             return nil
         end
     end

     while true do
         local victim = nearestenemy()
         if victim then
             victim:TakeDamage(3)
         end
         task.wait(.5)
     end

敌人死亡 0.6 秒后消失

请帮忙!

我查看了论坛,没有任何效果 我让塔不瞄准死去的敌人,没有改变 我检查了敌人是否有类人生物,没有任何效果(特定敌人是从具有类人生物的原始克隆敌人克隆而来)

lua roblox
1个回答
0
投票
local _,victim = pcall(nearestenemy)
© www.soinside.com 2019 - 2024. All rights reserved.