我正在尝试制作一个战斗系统,显然需要一个组合变量来跟踪我当前所处的组合
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 所示),但这些都没有真正帮助,任何帮助将不胜感激!
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)