You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Roblox报错:尝试对Instance与数值执行减法运算求排查

解决Roblox Lua脚本中“attempt to perform arithmetic (sub) on Instance and number”报错问题

问题描述

运行以下Roblox Lua脚本时,标记行出现报错:attempt to perform arithmetic (sub) on Instance and number,但另一辆使用完全相同代码的Taxi可正常运行。

原脚本代码

local HumanoidTaxi = script.Parent.Ally
local TaxiB = script.Parent.Damage
local Taxi = script.Parent
local TweenService = game:GetService("TweenService")
local Debounce = false
local Places = game.Workspace.PlacesAlly

TaxiB.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Zombie") then
        if not Debounce then
            Debounce = true
            local EnemyHumanoid = hit.Parent:FindFirstChild("Zombie")
            EnemyHumanoid.Health = EnemyHumanoid.Health - 25 ----------- 报错行
            HumanoidTaxi.Health = HumanoidTaxi.Health - EnemyHumanoid.Health
            wait(2)
            Debounce = false
        end
    end
end)

HumanoidTaxi.Died:Connect(function()
    Taxi:Destroy()
end)

for Place=1, #Places:GetChildren() do
    HumanoidTaxi:MoveTo(Places[Place].Position)
    HumanoidTaxi.MoveToFinished:Wait()
end

报错原因

报错核心是**EnemyHumanoid并非Humanoid实例**:

  • 代码中通过hit.Parent:FindFirstChild("Zombie")获取的对象,不是带有数值类型Health属性的Humanoid,而是其他类型的Instance(比如Zombie模型本身、或某个包含Health子实例的对象)。
  • 此时EnemyHumanoid.Health返回的是一个Instance而非数字,尝试用Instance减去数字25就会触发类型错误。
  • 另一辆Taxi能正常运行,是因为对应的Zombie对象中,名为"Zombie"的恰好是Humanoid实例,其Health属性为数值,减法运算合法。

解决方案

方案1:按类型查找Humanoid(推荐)

直接通过FindFirstChildOfClass查找Humanoid实例,避免名称混淆:

local HumanoidTaxi = script.Parent.Ally
local TaxiB = script.Parent.Damage
local Taxi = script.Parent
local Debounce = false
local Places = game.Workspace.PlacesAlly

TaxiB.Touched:Connect(function(hit)
    -- 按类型查找Humanoid,无需依赖对象名称
    local EnemyHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    if EnemyHumanoid and not Debounce then
        Debounce = true
        EnemyHumanoid.Health = EnemyHumanoid.Health - 25
        HumanoidTaxi.Health = HumanoidTaxi.Health - EnemyHumanoid.Health
        task.wait(2) -- 用task.wait替代wait,性能更稳定
        Debounce = false
    end
end)

HumanoidTaxi.Died:Connect(function()
    Taxi:Destroy()
end)

-- 用ipairs遍历子对象,避免索引越界问题
for _, Place in ipairs(Places:GetChildren()) do
    HumanoidTaxi:MoveTo(Place.Position)
    HumanoidTaxi.MoveToFinished:Wait()
end

方案2:保留名称查找但增加类型验证

如果必须通过名称"Zombie"查找,需增加Humanoid类型检查:

TaxiB.Touched:Connect(function(hit)
    local EnemyHumanoid = hit.Parent:FindFirstChild("Zombie")
    -- 确认找到的对象是Humanoid实例
    if EnemyHumanoid and EnemyHumanoid:IsA("Humanoid") and not Debounce then
        Debounce = true
        EnemyHumanoid.Health = EnemyHumanoid.Health - 25
        HumanoidTaxi.Health = HumanoidTaxi.Health - EnemyHumanoid.Health
        task.wait(2)
        Debounce = false
    end
end)

内容的提问来源于stack exchange,提问作者AronasRR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 05:47:46