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!