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

Damage multiplier失效:Roblox工具蓄力仅输出BaseDamage问题求助

问题分析与修复方案

核心问题

你的代码里有几个关键错误导致乘数不生效:

  1. dmgM变量的错误使用:一开始读取的是DMGmultiplier.Value的数值而非实例引用,后续修改dmgM只是改动局部变量,且Deactivated事件里直接把dmgM重置为1,导致攻击触发时乘数已被清零。
  2. combo未初始化:combo变量无初始值(默认nil),Activated里的if combo == 0判断永远不成立,蓄力逻辑根本没执行。
  3. 缺失关键变量定义:baseDMG、idle、hold1、hold2均未定义,运行时会报错中断逻辑。
  4. 蓄力乘数未保留到攻击阶段:Deactivated里立刻重置dmgM,但攻击的Touched事件在延迟后才触发,此时乘数已被重置为1。

修复后的代码

-- Essentials
local tool = script.Parent
repeat task.wait() until tool.Parent:FindFirstChild("Humanoid")

local player = game.Players:GetPlayerFromCharacter(tool.Parent)
local humanoid = tool.Parent.Humanoid
local animator = humanoid.Animator

-- 定义基础伤害(自行修改数值)
local baseDMG = 10
-- 初始化连击数
local combo = 0
-- 引用数值实例而非直接取Value
local dmgMInstance = player.PlayerGui.Stats.DamageMultiplier.DMGmultiplier
local dmgMT = player.PlayerGui.Stats.DamageMultiplier

-- 加载动画(替换为你的动画ID)
local idle = animator:LoadAnimation(tool:WaitForChild("IdleAnim"))
local hold1 = animator:LoadAnimation(tool:WaitForChild("Hold1Anim"))
local hold2 = animator:LoadAnimation(tool:WaitForChild("Hold2Anim"))

-- 保留当前蓄力乘数,用于攻击阶段计算
local currentChargeMultiplier = 1

tool.Activated:Connect(function()
    local holding = true
    currentChargeMultiplier = 1
    dmgMInstance.Value = currentChargeMultiplier
    dmgMT.Visible = true

    if combo == 0 then
        idle:Stop()
        hold1:Play()
        repeat
            -- 蓄力乘数上限为2
            currentChargeMultiplier = math.min(currentChargeMultiplier + 0.01, 2)
            dmgMInstance.Value = currentChargeMultiplier
            dmgMT.Text = "x" .. string.format("%.2f", currentChargeMultiplier)
            task.wait(0.01)
        until not holding or currentChargeMultiplier >= 2
    elseif combo == 1 then
        idle:Stop()
        hold2:Play()
        repeat
            currentChargeMultiplier = math.min(currentChargeMultiplier + 0.01, 2)
            dmgMInstance.Value = currentChargeMultiplier
            dmgMT.Text = "x" .. string.format("%.2f", currentChargeMultiplier)
            task.wait(0.01)
        until not holding or currentChargeMultiplier >= 2
    end
end)

tool.Deactivated:Connect(function()
    holding = false
    hold1:Stop()
    hold2:Stop()
    dmgMT.Visible = false

    if combo == 0 then
        tool.Enabled = false
        tool.Hitbox.CanTouch = true
        task.wait(0.3)
        idle:Play()
        tool.Enabled = true
        tool.Hitbox.CanTouch = false
        combo += 1
    elseif combo == 1 then
        tool.Enabled = false
        tool.Hitbox.CanTouch = true
        task.wait(0.5)
        idle:Play()
        tool.Enabled = true
        tool.Hitbox.CanTouch = false
        combo = 0 -- 连击重置,按需调整
    end
    -- 攻击结束后再重置乘数
    task.delay(0.5, function()
        currentChargeMultiplier = 1
        dmgMInstance.Value = currentChargeMultiplier
    end)
end)

tool.Hitbox.Touched:Connect(function(hit)
    local hitHumanoid = hit.Parent:FindFirstChild("Humanoid")
    if not hitHumanoid then return end

    local tgrPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
    if tgrPlayer then return end -- 不攻击玩家

    local humRP = hit.Parent:FindFirstChild("HumanoidRootPart")
    if humRP then
        humRP.AssemblyLinearVelocity = Vector3.new(50, 25, 0)
    end
    tool.Handle.Hit:Play()
    -- 使用保留的蓄力乘数计算总伤害
    local total = baseDMG * currentChargeMultiplier
    hitHumanoid:TakeDamage(total)
    print("总伤害:", total)
    tool.Hitbox.CanTouch = false
end)

关键修改说明

  • 变量初始化:给combo设初始值0,定义baseDMG并加载所需动画。
  • 蓄力乘数保留:新增currentChargeMultiplier变量,蓄力时更新它,不在Deactivated里立刻重置,等攻击结束后再清零,确保攻击触发时能用到正确乘数。
  • 引用数值实例:直接修改dmgMInstance.Value同步GUI数值,避免操作局部变量副本。
  • 逻辑顺序调整:延后乘数重置时机,避免攻击前清零。
  • 健壮性优化:增加空值判断,防止因找不到实例报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 17:14:51