Combo不会高于1,尝试制作战斗系统(roblox)

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

我正在尝试制作一个战斗系统,显然需要一个组合变量来跟踪我当前所处的组合

game.ReplicatedStorage.Remotes.M1.OnServerEvent:Connect(function(player, Mouse)
    print("Received M1 from the Client")
    local combo = 0
    local startTime = tick()

    if combo == 3 then
        print("M1 Combo!")
        combo = 0
    end

    if tick() - startTime >= 2 then
        combo = 0
    end

    combo = combo + 1
    print(combo)
end)

但它似乎永远不会高于 1 或永远不会在组合中添加 1

我尝试将打印调试添加到脚本中(如组合 = 组合 + 1 所示),但这些都没有真正帮助,任何帮助将不胜感激!

lua roblox
1个回答
0
投票

combo
在事件处理程序中被声明为局部变量,因此每次接收到事件时,它都会从 0 开始
combo
。相反,
combo
startTime
应在函数外部声明。

其他一些小事情是

tick()
很快就会被弃用,所以应该使用
os.time()
来代替。此外,
combo = combo + 1
可以缩写为
combo += 1
,并且
game:GetService("{serviceName}")
game.{serviceName}
推荐
。下面,使用
lastHit
代替
startTime
,因为它跟踪最后一次点击的时间。

所有这些修复后的脚本:

local combo = 0
local lastHit = os.time()

game:GetService("ReplicatedStorage").Remotes.M1.OnServerEvent:Connect(function(player, Mouse)
    print("Received M1 from the Client")
    combo += 1

    if os.time() - lastHit >= 2 then
        combo = 0
    end

    lastHit = os.time()

    if combo == 3 then
        print("M1 Combo!")
        combo = 0
    end

    print(combo)
end)
© www.soinside.com 2019 - 2024. All rights reserved.