AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79470375
Accepted
GrandpaKartul
GrandpaKartul
Asked: 2025-02-27 00:12:10 +0800 CST2025-02-27 00:12:10 +0800 CST 2025-02-27 00:12:10 +0800 CST

尝试使用“humanoid”索引 nil(Roblox Studio)

  • 772

我正在尝试编写一个炮塔代码,当敌人进入炮塔射程时向敌人射击,但代码有时会中断并给出错误信息。Script:31: attempt to index nil with 'Humanoid' 这种情况很少发生,只有当僵尸死亡时才会发生。不仅如此,似乎有些子弹造成的伤害比实际伤害要大得多。

head = script.Parent.PrimaryPart

local function triggershoot(part)
    local beam = Instance.new("Part",script.Parent)

    beam.Anchored = true
    beam.CanCollide = false
    beam.Shape = "Block"
    beam.CFrame = head.CFrame
    beam.Size = Vector3.new(0.5,0.5,0.5)
    beam.Material = "Plastic"
    beam.BrickColor = BrickColor.new("Dark taupe")

    beam.CFrame = CFrame.lookAt(head.Position,part.Position)
    for i = 0, 50, 1 do
        wait()
        beam.CFrame = beam.CFrame + beam.CFrame.LookVector      
        beam.Touched:Connect(function(plr)
            if plr.Parent:HasTag("enemy") then
                plr.Parent.Humanoid:TakeDamage(3)
                beam:Destroy()
            end
        end)
    end
end

while true do
    wait()
    local parts = workspace:GetPartsInPart(script.Parent.zone)
    for _, part in pairs(parts) do
        if part:HasTag("enemy") and part.Parent.Humanoid.Health > 0 then

            local update = CFrame.lookAt(head.Position,part.Position)
            head.CFrame = update
            coroutine.wrap(triggershoot)(part)
            wait(1)
        end
    end
end
lua
  • 2 2 个回答
  • 47 Views

2 个回答

  • Voted
  1. Best Answer
    user15611379
    2025-02-27T01:59:58+08:002025-02-27T01:59:58+08:00

    尝试将触摸事件函数移到 forloop 之外

    head = script.Parent.PrimaryPart
    
    local function triggershoot(part)
        local beam = Instance.new("Part",script.Parent)
    
        beam.Anchored = true
        beam.CanCollide = false
        beam.Shape = "Block"
        beam.CFrame = head.CFrame
        beam.Size = Vector3.new(0.5,0.5,0.5)
        beam.Material = "Plastic"
        beam.BrickColor = BrickColor.new("Dark taupe")
    
        beam.CFrame = CFrame.lookAt(head.Position,part.Position)
        beam.Touched:Connect(function(plr)
            local char=plr.Parent
            local hum=char and char:FindFirstChildOfClass"Humanoid"
            if char:HasTag("enemy") then
                hum:TakeDamage(3)
                beam:Destroy()
            end
        end)
        for i = 0, 50, 1 do
            wait()
            beam.CFrame = beam.CFrame + beam.CFrame.LookVector      
        end
        beam:Destroy()
    end
    while true do
        wait()
        local parts = workspace:GetPartsInPart(script.Parent.zone)
        for _, part in ipairs(parts) do
            local h=part.Parent;h=h and h:FindFirstChildOfClass"Humanoid"
            if part:HasTag"enemy"and h and h.Health > 0 then
                local update = CFrame.lookAt(head.Position,part.Position)
                head.CFrame = update
                coroutine.wrap(triggershoot)(part)
                wait(1)
                break
            end
        end
    end

    • 0
  2. Kemal
    2025-02-28T05:10:01+08:002025-02-28T05:10:01+08:00

    如果我是正确的,那么您在脚本中遇到了两个主要问题:

    1. “尝试使用‘Humanoid’对 nil 进行索引”

    发生此错误是因为part.Parent.Humanoid炮塔nil尝试访问它。这通常发生在僵尸死亡时,其人形物体在炮塔检查其生命值或施加伤害之前被摧毁。

    解决方案:在尝试使用之前添加检查以确保 Humanoid 存在。

    修改此部分:

    if part:HasTag("enemy") and part.Parent.Humanoid.Health > 0 then
    

    对此:

    local humanoid = part.Parent:FindFirstChild("Humanoid")
    if part:HasTag("enemy") and humanoid and humanoid.Health > 0 then
    
    1. 子弹伤害多次施加

    您正在循环内连接 beam.Touched 事件,这意味着它会在每一帧中重复连接。这可能会导致子弹多次造成伤害。

    解决方案:将 .Touched 事件移出循环,以便它只连接一次。

    固定代码:

    head = script.Parent.PrimaryPart
    
    local function triggershoot(part)
        local beam = Instance.new("Part")
        beam.Parent = script.Parent
        beam.Anchored = true
        beam.CanCollide = false
        beam.Shape = Enum.PartType.Block
        beam.CFrame = head.CFrame
        beam.Size = Vector3.new(0.5, 0.5, 0.5)
        beam.Material = Enum.Material.Plastic
        beam.BrickColor = BrickColor.new("Dark taupe")
    
        beam.CFrame = CFrame.lookAt(head.Position, part.Position)
    
        local function onHit(plr)
            if plr.Parent:HasTag("enemy") then
                local humanoid = plr.Parent:FindFirstChild("Humanoid")
                if humanoid then
                    humanoid:TakeDamage(3)
                    beam:Destroy()
                end
            end
        end
    
        beam.Touched:Connect(onHit)
    
        for i = 1, 50 do
            beam.CFrame = beam.CFrame + beam.CFrame.LookVector
            wait()
        end
    
        beam:Destroy()
    end
    
    while true do
        wait()
        local parts = workspace:GetPartsInPart(script.Parent.zone)
    
        for _, part in pairs(parts) do
            local humanoid = part.Parent:FindFirstChild("Humanoid")
            if part:HasTag("enemy") and humanoid and humanoid.Health > 0 then
                head.CFrame = CFrame.lookAt(head.Position, part.Position)
                coroutine.wrap(triggershoot)(part)
                wait(1)
            end
        end
    end
    

    修复摘要:

    1. 访问之前先检查是否Humanoid 存在(FindFirstChild("Humanoid"))。
    2. 将事件 .Touched 移出循环,以便每个项目符号仅连接一次。
    3. 循环结束后销毁子弹,以防止不必要的部分留在游戏中。

    希望这能解决你们两个的问题!🚀

    可能有帮助的资源:

    1. 理解 FindFirstChild 以防止出现 Nil 错误

      • Roblox 开发者中心 -FindFirstChild方法 📖 FindFirstChild 文档
        • 在访问对象的属性(如)之前,此方法对于检查对象是否存在至关重要Humanoid.Health。
        • 它可以防止诸如“尝试使用‘Humanoid’索引 nil”之类的错误。
    2. 正确处理 Touched 事件

      • Roblox 开发者中心 - Touched 活动 📖 Touched 活动文档
        • 该.Touched事件可以多次触发,因此您需要为每个射弹连接一次。
        • 避免放入.Touched:Connect()循环,因为它会创建多个事件监听器,从而导致意外行为。
      • DevForum - 为什么 Touched 可以多次触发 📖 触摸事件行为主题
        • 解释事件的工作方式和常见错误,例如每颗子弹发射多个伤害事件。
    3. 用于 CFrame.lookAt() 瞄准

      • Roblox 开发者中心 - CFrame.lookAt 方法📖 CFrame.lookAt 文档
        • 此功能可帮助炮塔自动旋转至敌人方向。
        • 您可以通过示例了解其工作原理。
    4. 并行运行函数的协程基础知识

      • Roblox 开发者中心-coroutine.wrap 以及 coroutine.resume 📖 协程文档
        • 用于使炮塔射击而不暂停主循环。
        • 如果没有coroutine.wrap(triggershoot)(part),炮塔会等待每颗子弹发射完毕后再检查其他敌人。
    5. 正确的子弹移动技术

      • Roblox 开发者中心 - TweenService 实现子弹的平滑移动 📖 TweenService 文档
        • 无需使用循环(for i = 1, 50),TweenService 可以平滑地移动子弹,而不会影响性能。
    • 0

相关问题

  • 如何在 neovim 中触发 lsp 建议弹出窗口?

  • Lua if 语句总是转到 else 选项

  • Lua脚本在按下几个键后执行命令

  • 如果表中有多个元素,如何在 Lua 中检查?

  • Lua脚本结束TCP连接?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve