我制作了一个战斗系统(并在参考之前提出了一个不同的问题),并且如果我四处移动,碰撞箱总是会多次击中,我尝试使用去抖值使其停止,这对于其他人来说很常见,但它仍然不工作,代码如下:
local deb = game:GetService("Debris")
game.ReplicatedStorage.Remotes.M1.OnServerEvent:Connect(function(player, combo)
local debounce = false
--//Debug\\--
print("Current Combo: " .. combo)
--//Debug--\\
local anims = player.Character:WaitForChild("Anims")
local animator = player.Character.Humanoid:WaitForChild("Animator")
local combo1 = animator:LoadAnimation(anims.Combo1)
local combo2 = animator:LoadAnimation(anims.Combo2)
local combo3 = animator:LoadAnimation(anims.Combo3)
local combo4 = animator:LoadAnimation(anims.Combo4)
local endlag = 1
if combo == 1 then
combo1:Play()
elseif combo == 2 then
combo2:Play()
elseif combo == 3 then
combo3:Play()
elseif combo == 4 then
combo4:Play()
end
local character = player.Character
if character then
local hrp = character:FindFirstChild("HumanoidRootPart")
if hrp then
local damagePart = Instance.new("Part")
damagePart.Material = "ForceField"
damagePart.Parent = workspace
damagePart.Size = Vector3.new(5, 5, 5)
damagePart.Anchored = false
damagePart.CanCollide = false
damagePart.Transparency = 0
damagePart.Name = "DamagePart"
deb:AddItem(damagePart, 0.4)
local weld = Instance.new("Weld")
weld.Part0 = hrp
weld.Part1 = damagePart
weld.C0 = CFrame.new(0, 0, 0) -- Adjust the offset as needed
weld.Parent = hrp
damagePart.Touched:Connect(function(otherPart)
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
if humanoid and humanoid.Parent ~= player.Character and not debounce then
spawn(function()
debounce = true
wait(1)
debounce = false
end)
local stunned = otherPart.Parent:FindFirstChild("Stunned")
if not stunned then
stunned = Instance.new("BoolValue")
stunned.Name = "Stunned"
stunned.Parent = otherPart.Parent
end
local blocking = otherPart.Parent:FindFirstChild("Blocking")
if otherPart.Parent:FindFirstChild("Blocking").Value == false then
game.ReplicatedStorage.Sounds:WaitForChild("Hit"):Play()
humanoid:TakeDamage(4)
elseif otherPart.Parent:FindFirstChild("Blocking").Value == true then
game.ReplicatedStorage.Sounds:WaitForChild("BlockHit"):Play()
print("Opponent is blocking, no damage")
return
end
if not blocking then
stunned = Instance.new("BoolValue")
stunned.Name = "Blocking"
stunned.Parent = otherPart.Parent
end
stunned.Value = true
humanoid.WalkSpeed = 0
wait(endlag)
if stunned then
humanoid.WalkSpeed = 16
stunned.Value = false
end
end
end)
end
end
end)
game.ReplicatedStorage.Remotes:WaitForChild("Block").OnServerEvent:Connect(function(player, blocking)
if blocking == true then
player.Character.Blocking.Value = true
player.Character.Humanoid.WalkSpeed = 6
elseif blocking == false then
player.Character.Blocking.Value = false
player.Character.Humanoid.WalkSpeed = 16
end
end)
我尝试将去抖动设置为全局变量,我尝试将其设置为在服务器事件上发生时位于远程内部,但仍然没有任何效果。请帮忙!
Debounce 应在事件处理程序之外声明。当事件被触发时,如果 debounce 当前为 true,则应返回该函数。另外,应该在服务器上跟踪组合,并使用您上一个问题中的功能。
local debounce = false
game:GetService("ReplicatedStorage").Remotes.M1.OnServerEvent:Connect(function(player)
if debounce then return end
debounce = true
...
task.wait(1) -- ensure that debounce is true for at least one second
debounce = false
end)