Help with punch script

Hi, I’m making a punch script in startercharacterscripts but it does damage about .5 seconds after actually hitting the dummy. How can I fix this?

video:
https://imgur.com/a/dpxB2Kb

local Enabled = true
local Combo = 1
local RunService = game:GetService("RunService")
local Region
local RTable

script.Parent.OnServerEvent:connect(function(Player, Action)    
local c = Player.Character
	if Enabled == false then return end
	if Action == "Combat" then
	    local Anim1 = c.Humanoid:LoadAnimation(script.Punch1)
		local Anim2 = c.Humanoid:LoadAnimation(script.Punch2)
		Enabled = false
		if Combo == 1 then
			Combo = 2
			Anim1:Play()
			script.Event:Fire()
		elseif Combo == 2 then
			Combo = 1
			Anim2:Play()
			script.Event:Fire()
		end
		wait(.2)
		coroutine.resume(coroutine.create(function()
            while Anim1.IsPlaying or Anim2.IsPlaying and RunService.Stepped:wait() do
				wait(.01)
				Region = Region3.new(c.HumanoidRootPart.CFrame*CFrame.new(0,0,-2).p - Vector3.new(2,2,2),c.HumanoidRootPart.CFrame*CFrame.new(0,0,-2).p + Vector3.new(2,2,2)) -- region3 -- region3
				RTable = workspace:FindPartsInRegion3(Region, nil, 20)
				for i,v in pairs(RTable) do
					break
				end
			end
			for i,v in pairs(RTable) do
				RTable = nil
				if v.Parent:findFirstChild("Humanoid") and v.Parent:findFirstChild("Deb") == nil and v.Parent ~= c then
					v.Parent.Humanoid:TakeDamage(4)
					
					local Deb = Instance.new("BoolValue", v.Parent)
					Deb.Name = "Deb"
					game.Debris:AddItem(Deb,0.2)
					
					local S = Instance.new("Sound", v)
					S.SoundId = "rbxassetid://131237241"
					S.PlaybackSpeed = math.random(80,120)/100
					S:Play()
					
					local FX = Instance.new("Part", workspace.FX)
					FX.Name = "CombatHit"
					FX.Transparency = 0
					FX.CanCollide = false
					FX.Anchored = true
					FX.Material = "ForceField"
					FX.Color = Color3.new(1,0,0)
					FX.Size = Vector3.new(1,1,1)
					
					local SM = Instance.new("SpecialMesh", FX)SM.MeshType = "Sphere"
					SM.Scale = Vector3.new(0,0,0)
					FX.CFrame = v.Parent.HumanoidRootPart.CFrame*CFrame.new(math.random(-20,20)/10,math.random(-20,20)/10,math.random(-20,20)/10)
					
					local BV = Instance.new("BodyVelocity", v)
					BV.maxForce = Vector3.new(25000,25000,25000)
					BV.Velocity = c.HumanoidRootPart.CFrame.lookVector*5
					game.Debris:AddItem(BV,0.2)
				end
			end
		end))
		wait(0.25)
		Enabled = true
		if script.Parent.Parent.Parent.Humanoid.Health < 1 then
			script.Parent.Parent:Destroy()
		end
	end
end)
3 Likes

All the waits are the reason when your punching its waiting .2 and so on with more waits I think the problem might be the waits in the script correct me if im wrong

1 Like

I’m pretty sure it’s because of
while Anim1.IsPlaying…
because it waits for the animation to finish playing before it can continue to do the rest of the script
but I don’t know how to fix this

3 Likes

dont do the wait just make sure the animation is perfectly timed like if its long bring it more up in the code and if its short start it more down the script

1 Like

Might be because the animation track doesn’t stop after punching because you didn’t cut it down correctly, does the animation actually end after the punch or does it still play?

1 Like

It does damage right when the punching animation ends
but i need it do do damage at about half the punching animation
like when the punch hits

1 Like

I will include a video soon //

1 Like

so try wait(animation time / 2)

1 Like

I think the wait are slowing it down or it could be the animation

2 Likes

you should add a keyframe event in your animation and then detect it in the script then

1 Like

Ok so this is where I’m at right now

local Enabled = true
local Combo = 1
local RunService = game:GetService("RunService")
local Region
local RTable

script.Parent.OnServerEvent:connect(function(Player, Action)    
local c = Player.Character
	if Enabled == false then return end
	if Action == "Combat" then
	    local Anim1 = c.Humanoid:LoadAnimation(script.Punch1)
		local Anim2 = c.Humanoid:LoadAnimation(script.Punch2)
		Enabled = false
		if Combo == 1 then
			Combo = 2
			Anim1:Play()
			script.Event:Fire()
		elseif Combo == 2 then
			Combo = 1
			Anim2:Play()
			script.Event:Fire()
		end
		wait(.25)
		coroutine.resume(coroutine.create(function()
            while Anim1.IsPlaying or Anim2.IsPlaying and Anim1.Length < Anim1.Length/2 or Anim2.Length < Anim2.Length/2 and RunService.Stepped:wait() do -- While the animation is playing and we wait for the next frame (Approx. 1/60th a second)
				wait(.01)
				Region = Region3.new(c.HumanoidRootPart.CFrame*CFrame.new(0,0,-2).p - Vector3.new(2,2,2),c.HumanoidRootPart.CFrame*CFrame.new(0,0,-2).p + Vector3.new(2,2,2)) -- region3
				RTable = workspace:FindPartsInRegion3(Region, nil, 20)
			end
			for i,v in pairs(RTable) do
				if v.Parent:findFirstChild("Humanoid") and v.Parent:findFirstChild("Deb") == nil and v.Parent ~= c then
					v.Parent.Humanoid:TakeDamage(4)
					local Deb = Instance.new("BoolValue", v.Parent)
					Deb.Name = "Deb"
					game.Debris:AddItem(Deb,0.2)
					
					local S = Instance.new("Sound", v)
					S.SoundId = "rbxassetid://131237241"
					S.PlaybackSpeed = math.random(80,120)/100
					S:Play()
					
					local FX = Instance.new("Part", workspace.FX)
					FX.Name = "CombatHit"
					FX.Transparency = 0
					FX.CanCollide = false
					FX.Anchored = true
					FX.Material = "ForceField"
					FX.Color = Color3.new(1,0,0)
					FX.Size = Vector3.new(1,1,1)
					
					local SM = Instance.new("SpecialMesh", FX)SM.MeshType = "Sphere"
					SM.Scale = Vector3.new(0,0,0)
					FX.CFrame = v.Parent.HumanoidRootPart.CFrame*CFrame.new(math.random(-20,20)/10,math.random(-20,20)/10,math.random(-20,20)/10)
					
					local BV = Instance.new("BodyVelocity", v)
					BV.maxForce = Vector3.new(25000,25000,25000)
					BV.Velocity = c.HumanoidRootPart.CFrame.lookVector*5
					game.Debris:AddItem(BV,0.2)
				end
			end
		end))
		wait(0.25)
		Enabled = true
		if script.Parent.Parent.Parent.Humanoid.Health < 1 then
			script.Parent.Parent:Destroy()
		end
	end
end)

currently only the right hand works as intended, I don’t know why the left hand doesn’t work

2 Likes

if you want your hitbox to be summoned halfway through the punch add a keyframe event in the animation editor where you would like, name it “Hitbox” or something, and then detect in the animation when that keyframe event is reached, when its reached create hitbox.

1 Like

Can animation events be used in a script, i think they only work in localscripts

1 Like

yes they can be detected in scripts, you create them via animation editor though.

1 Like

Hi again I fixed this
Here’s the fix

		wait(.2)
		coroutine.resume(coroutine.create(function()
	        while Anim1.IsPlaying or Anim2.IsPlaying and RunService.Stepped:wait() do -- While the animation is playing and we wait for the next frame (Approx. 1/60th a second)
				wait(.01)
				Region = Region3.new(c.HumanoidRootPart.CFrame*CFrame.new(0,0,-2).p - Vector3.new(2,2,2),c.HumanoidRootPart.CFrame*CFrame.new(0,0,-2).p + Vector3.new(2,2,2)) -- region3 -- region3
				RTable = workspace:FindPartsInRegion3(Region, nil, 20)
				if run then
					run = false
					print("E")
					break
				end
				for i,v in pairs(RTable) do
					if v.Parent:findFirstChild("Humanoid") and v.Parent:findFirstChild("Deb") == nil and v.Parent ~= c then
						run = true
					end
				end
			end
			for i,v in pairs(RTable) do
				RTable = false
				if v.Parent:findFirstChild("Humanoid") and v.Parent:findFirstChild("Deb") == nil and v.Parent ~= c then
					v.Parent.Humanoid:TakeDamage(4)
					local Deb = Instance.new("BoolValue", v.Parent)
					Deb.Name = "Deb"
					game.Debris:AddItem(Deb,0.2)
					
					local S = Instance.new("Sound", v)
					S.SoundId = "rbxassetid://131237241"
					S.PlaybackSpeed = math.random(80,120)/100
					S:Play()
					
					local FX = Instance.new("Part", workspace.FX)
					FX.Name = "CombatHit"
					FX.Transparency = 0
					FX.CanCollide = false
					FX.Anchored = true
					FX.Material = "ForceField"
					FX.Color = Color3.new(1,0,0)
					FX.Size = Vector3.new(1,1,1)
					
					local SM = Instance.new("SpecialMesh", FX)SM.MeshType = "Sphere"
					SM.Scale = Vector3.new(0,0,0)
					FX.CFrame = v.Parent.HumanoidRootPart.CFrame*CFrame.new(math.random(-20,20)/10,math.random(-20,20)/10,math.random(-20,20)/10)
					
					local BV = Instance.new("BodyVelocity", v)
					BV.maxForce = Vector3.new(25000,25000,25000)
					BV.Velocity = c.HumanoidRootPart.CFrame.lookVector*5
					game.Debris:AddItem(BV,0.2)
				end
			end
		end))
1 Like