local rp = game.ReplicatedStorage
local recoil = game.ReplicatedStorage.PunchValues.Recoil
local punchCount = game.ReplicatedStorage.PunchValues.PunchCount
recoil.Value = 0
local function Punch(player)
if recoil.Value == 1 then
return
end
recoil.Value = 1
local punchAnimation = player.Character.Humanoid.Animator:LoadAnimation(script.BrawlerRightPunch)
punchAnimation:Play()
punchAnimation.KeyframeReached:Connect(function(keyframeName)
if keyframeName ~= "Damage" then
return
end
player.Character.RightLowerArm.Touched:connect(function(hit)
if hit.Name == "Hitbox" and punchCount.Value == 0 then
print("L")
punchCount.Value = 1
end
end)
player.Character.RightUpperArm.Touched:connect(function(hit)
if hit.Name == "Hitbox" and punchCount.Value == 0 then
print("U")
punchCount.Value = 1
end
end)
end)
wait(1)
recoil.Value = 0
punchCount.Value = 0
end
rp.Punch.OnServerEvent:Connect(Punch)
while wait() do
print(punchCount.Value)
end
When I punch but dont touch the hitbox, and then i run to the hitbox, it still registers as a punch. I need help please.
You can use :GetPartsInPart. I could also optimize this further if you can tell me what part is named hitbox (just send a picture of explorer).
Code:
local rp = game.ReplicatedStorage
local recoil = game.ReplicatedStorage.PunchValues.Recoil
local punchCount = game.ReplicatedStorage.PunchValues.PunchCount
recoil.Value = 0
local function Punch(player)
if recoil.Value == 1 then
return
end
recoil.Value = 1
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Exclude
overlapParams.FilterDescendantsInstances = {player.Character}
local punchAnimation = player.Character.Humanoid.Animator:LoadAnimation(script.BrawlerRightPunch)
punchAnimation:Play()
punchAnimation.KeyframeReached:Connect(function(keyframeName)
if keyframeName ~= "Damage" then
return
end
if punchCount.Value == 0 then
for i = 1, 3 do
local partsTouching = workspace:GetPartsInPart(player.Character.RightLowerArm, overlapParams)
table.insert(partsTouching, workspace:GetPartsInPart(player.Character.RightUpperArm, overlapParams))
for i, part in partsTouching do
if part.Name == "Hitbox" then
punchCount.Value = 1
return
end
end
end
task.wait(0.5)
end
end)
wait(1)
recoil.Value = 0
punchCount.Value = 0
end
rp.Punch.OnServerEvent:Connect(Punch)
while wait() do
print(punchCount.Value)
end
The task.wait(0.5) makes it wait after every check. You can specify the amount of checks via the 3 value.
Oh, I just realized that using your original script would be more efficient. You just need to disconnect the .Touched function.
Code:
local rp = game.ReplicatedStorage
local recoil = game.ReplicatedStorage.PunchValues.Recoil
local punchCount = game.ReplicatedStorage.PunchValues.PunchCount
recoil.Value = 0
local function Punch(player)
if recoil.Value == 1 then
return
end
recoil.Value = 1
local punchAnimation = player.Character.Humanoid.Animator:LoadAnimation(script.BrawlerRightPunch)
punchAnimation:Play()
punchAnimation.KeyframeReached:Connect(function(keyframeName)
if keyframeName ~= "Damage" then
return
end
local rightTouched = player.Character.RightLowerArm.Touched:connect(function(hit)
if hit.Name == "Hitbox" and punchCount.Value == 0 then
print("L")
punchCount.Value = 1
end
end)
local leftTouched = player.Character.RightUpperArm.Touched:connect(function(hit)
if hit.Name == "Hitbox" and punchCount.Value == 0 then
print("U")
punchCount.Value = 1
end
end)
task.wait(1)
rightTouched:Disconnect()
leftTouched:Disconnect()
end)
wait(1)
recoil.Value = 0
punchCount.Value = 0
end
rp.Punch.OnServerEvent:Connect(Punch)
while wait() do
print(punchCount.Value)
end