I’m trying to make sure that the humanoid doesn’t lose life when I’m not punching even if I’m not punching humanoid is still losing. My hitbox is perfect that’s not the problem it’s like the event is still playing in the background for no reason
I tried disconnecting the event not working though
Client Code
local Services = {
UserInput = (game:FindService("UserInputService") or game:GetService("UserInputService")),
ContextAction = (game:FindService("ContextActionService") or game:GetService("ContextActionService"))
}
local Events = {
Punch = (game.ReplicatedStorage:FindFirstChild("Punch"))
}
local ReplicatedStorage = game.ReplicatedStorage
local CanPunch = true
local humanoid = script.Parent.Humanoid
local CurrentPlayer = nil
local Connection = nil
local Character = script.Parent
local LocalPlayer = game.Players.LocalPlayer
function punchMobile()
if CanPunch == true then
CanPunch = false
local defaults = game.Players.LocalPlayer:WaitForChild("DefaultAnimations")
local randomIndex = math.random(1, #defaults:GetChildren())
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. defaults:GetChildren()[randomIndex].Value
-- animation.AnimationId = "rbxassetid://16164866387"
local YourAnimationTrack = humanoid.Animator:LoadAnimation(animation)
YourAnimationTrack.Priority = Enum.AnimationPriority.Action
YourAnimationTrack:Play()
wait(YourAnimationTrack.Length)
Events.Punch:FireServer()
CanPunch = true
end
end
if Services.UserInput.TouchEnabled then
local PunchMobileButton = Services.ContextAction:BindAction("Bench", punchMobile, true)
Services.ContextAction:SetPosition("Bench", UDim2.new(0.62, -25, 0.30, -25))
Services.ContextAction:SetImage("Bench", "rbxassetid://16199341429")
end
Services.UserInput.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if CanPunch == true then
CanPunch = false
local defaults = game.Players.LocalPlayer:WaitForChild("DefaultAnimations")
if #defaults:GetChildren() > 1 then
local randomIndex = math.random(1, #defaults:GetChildren())
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. defaults:GetChildren()[randomIndex].Value
-- animation.AnimationId = "rbxassetid://16164866387"
local YourAnimationTrack = humanoid.Animator:LoadAnimation(animation)
YourAnimationTrack.Priority = Enum.AnimationPriority.Action
YourAnimationTrack:Play()
wait(YourAnimationTrack.Length)
Events.Punch:FireServer()
CanPunch = true
else
local randomIndex = math.random(1, #defaults:GetChildren())
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. defaults:GetChildren()[randomIndex].Value
-- animation.AnimationId = "rbxassetid://16164866387"
local YourAnimationTrack = humanoid.Animator:LoadAnimation(animation)
YourAnimationTrack.Priority = Enum.AnimationPriority.Action
YourAnimationTrack:Play()
wait(YourAnimationTrack.Length)
Events.Punch:FireServer()
CanPunch = true
end
end
end
end)
Server code :
local punchEvent = game.ReplicatedStorage.Punch
local debounce = false
local Connection = nil
local TouchedConnection = nil
local CurrentPlayer = nil
local Connection = nil
local counter = 0
local Properties = {
Damage = 10
}
local function OnPlayerPunch(player)
CurrentPlayer = game.Workspace:FindFirstChild(player.Name)
local LeftLowerArm = CurrentPlayer.LeftLowerArm
local LeftHand = CurrentPlayer.LeftHand
print("PLAYER PUNCHED")
print(Connection.Connected)
LeftLowerArm.Touched:Connect(function(hit)
local Hum,FF = hit.Parent:FindFirstChildOfClass("Humanoid"),hit.Parent:FindFirstChildOfClass("ForceField")
if not Hum or FF or Hum.Health <= 0 then return end
print("nand"..hit.Parent.Name)
print("nand"..hit.Name)
if not debounce then
debounce = true
if hit.Parent.Name ~= "El Pueblo" then
local current_sound = game.Workspace.Audio.PunchHumanoid
current_sound:Play()
Hum:TakeDamage(Properties.Damage)
end
wait(1)
debounce = false
end
end)
LeftHand.Touched:Connect(function(hit)
local Hum,FF = hit.Parent:FindFirstChildOfClass("Humanoid"),hit.Parent:FindFirstChildOfClass("ForceField")
if not Hum or FF or Hum.Health <= 0 then return end
if not debounce then
debounce = true
if hit.Parent.Name ~= "El Pueblo" then
local current_sound = game.Workspace.Audio.PunchHumanoid
current_sound:Play()
Hum:TakeDamage(Properties.Damage)
end
wait(1)
debounce = false
end
end)
end
Connection = punchEvent.OnServerEvent:Connect(OnPlayerPunch)