Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to fix onTouch Function Delay?

https://www.youtube.com/watch?v=7Cf5Olp3tJ0

The Sword was suposed to damage any humanoid that it touches on the first swing but the damage function is being activated with a delay for some reason and it is making it only damage things ontouch after the animation is done.

The Code:

  if keyPressed == Enum.KeyCode.Z and Toggled == true and AnimOn == false and ZAttackCD == true then
   AnimOn = true
   ZAttackCD = false
   Humanoid.WalkSpeed = 0
   ZAttack:Play()
   wait(0.25)
   TParts.Trail.Enabled = true
   local Conn
   Conn = TParts.Touched:Connect(function(hit)	  
   if hit.Parent:FindFirstChild("Humanoid") then
    if not hit.Parent.Humanoid:FindFirstChild("BValue") then
     local dtag = BValue:Clone()
      dtag.Parent = hit.Parent.Humanoid
     hit.Parent.Humanoid:TakeDamage(15)
     game:GetService("Debris"):AddItem(dtag,1)
     wait(1)
     Conn:Disconnect()
    end
   end
  end)
   wait(0.85)
   TParts.Trail.Enabled = false
   ZAttack.Stopped:Wait()
   AnimOn = false
   Humanoid.WalkSpeed = WalkSpeed
   wait(10)
   ZAttackCD = true

Summing up:I want to make it so the sword damages while the char plays the animation of the first swing, not after nor before.

Any help would be appreciated, thanks.

1 answer

✓ Accepted answer

Hey! Did you know that you could put events in the animation editor?

You can use this feature to make this line of script work:

canhit = true
AnimationTrack:GetMarkerReachedSignal("animationName"):Connect(function(argument)
	sword.Touched:Connect(function(hit)
	if canhit == true then
	hit.Parent:FindFirstChild("Humanoid"):TakeDamage(15)
	canhit = false
	else 
	print("already hit once")
       	end
	wait(animation timeout time)
	canhit = false
end

To add an animation event, click the pencil with a + using the roblox animation editor. There, you will set the name and the parameter and the marker will signal the function above when it hits the keyframe you put it at.

Orrrr you can just use this handy module made by Swordphin123

https://devforum.roblox.com/t/raycast-hitbox-module-for-all-your-melee-needs/374482

This discussion is preserved as it was. New answers, comments, and votes are closed.