Weapons only doing damage after reaching a keyframe

I am attempting to make a sword/knife which only hits after the animation reaches a keyframe using GetMarkerReachedSignal and Touched, but I have a problem. Touched only updates when the object first comes in contact when an object first comes in contact with it, meaning if the weapon doesn’t stop touching the thing it’s attacking, a second hit won’t register. For reference, here is the code I am currently using:

local tool = script.Parent --this is the tool
local cooldownDuration = 0 --how long the cooldown should be (for convenience purposes)
local cooldown = false --whether the weapon is on cooldown
local animation = script.SlashAnim --the animation for the attack
local damaging = false --whether the weapon is currently in a damaging state
local handle = tool.Handle --the handle of the tool
local backpack = tool.Parent --the backpack
local player = backpack.Parent --the player who owns the tool
local character = player.Character --the character of the player
local humanoid = character:WaitForChild("Humanoid") --the character's humanoid
local loadedAnim = humanoid:LoadAnimation(animation) --the attack animation, ready for use

tool.Equipped:Connect(function()
	cooldown = true
	wait(cooldownDuration + loadedAnim.Length)
	cooldown = false
end)
--this part is to make sure players can't switch between weapons to avoid cooldowns

tool.Activated:Connect(function()
	if humanoid and cooldown == false then 
		cooldown = true
		loadedAnim:Play()
		wait(loadedAnim.Length + cooldownDuration)
		cooldown = false
	end
end)
--this plays the animation of the slash and activates the cooldown

handle.Touched:Connect(function(part)  --activates when the weapon is touched
	if damaging == true then --if the weapon is currently in a damaging state
		local parent = part.Parent --parent is the parent of the part that the weapon touched
		if parent then --if parent exists
			local eneH = parent:FindFirstChild("Humanoid") 
			if eneH then --if the parent cointains a humanoid then
  	              --hit script which deals damage, removed to avoid confusion
			end	
		end			
	end
end)
    				
loadedAnim:GetMarkerReachedSignal("StabBegin"):Connect(function()
	damaging = true
end)
--when the animation reaches a marker called "StabBegin", set damaging to true

loadedAnim:GetMarkerReachedSignal("StabEnd"):Connect(function()
	damaging = false
end)
--when the animation reaches a marker called "StabEnd", set damaging to false

I’d appreciate it if you could help!

you can use BasePart:GetTouchingParts() at the exact moment of the marker event which gets you a table of all touching parts, i generally avoid physics based hit detection and use custom hitboxes, Egomoose’s RotatedRegion3 module is perfect for casting complicated shapes and getting all parts in an area, it even has a function to create a Region3 out of a parts CFrame and Size which you can use to cast a hitbox of the weapons blade part on impact

1 Like

Thanks for your suggestions! I’m not the greatest scripter, so I’d prefer to stick with default roblox functions rather than modules for now, but I’ll certainly try your first idea. :slight_smile:

1 Like

alas, this.

If you decide to go through the effort, this is a great way to get potentially accurate and quickly computable hitboxes for melee.

2 Likes