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

Roblox开发疑问:Touched事件无伤害效果,如何实现仅对Boss造成伤害?

仅对Boss对象造成伤害的代码修正

你的代码存在几个潜在问题,导致可能无法精准触发或误伤玩家,以下是修正后的代码及关键说明:

hitpunch.Touched:Connect(function(hit)
    -- 冷却中直接返回,避免重复触发
    if not debounceDMG then return end
    -- 先找到碰撞对象所属的Humanoid,确保是可受伤害的角色
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    if not humanoid then return end

    local character = humanoid.Parent
    -- 仅对名为Boss的模型生效,同时排除玩家自身角色
    if character.Name == "Boss" and character ~= Player.Character then
        debounceDMG = false
        humanoid.Health -= Player:GetAttribute("Forca")
        task.wait(0.1)
        debounceDMG = true
    end
end)

关键修改点:

  • 优先检查Humanoid:只有带有Humanoid的模型才是可被攻击的角色,避免误触场景中的其他模型部件
  • 明确排除玩家角色:通过character ~= Player.Character直接排除玩家自身,彻底避免误伤
  • 优化判断逻辑:把无效情况(冷却中、无Humanoid)放在最前面提前退出,减少不必要的后续判断,代码更高效
  • 简化冷却判断:用if not debounceDMG then return end替代原有的多条件判断,逻辑更清晰

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 06:15:39