Combat system help

Alright, so I were making a combat system for a genre I was working for, and everything was going fine, and I almost finished it. But now, I have to make people able to choose directions if they hold LeftMouseButton, and just randomly attack with LeftMouseButton. I’ve tried a lot of ideas that I had, and nothing successed. Well, actually, I managed to do something while writing this topic, but thats probably all I can. Everything works as intended, but when the player is releasing LeftMouseButton, the function runs, but the problem is, that each time I attack, the damage function runs twice.

LocalScript function for animation
elseif input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProcessed then
		if character:FindFirstChild(Tool.Name) then
			canPrepare = true

			if debounce == false then
				toolActivatedValue.Value = true
				if #playingAnimations == 0 then
					if Tool.Name == "Sword" then
						local Anims = {
							["Right"] = AttackRightShieldless;
							["Left"] = AttackLeftShieldless;
							["Up"] = AttackOverheadShieldless;
							["Bottom"] = AttackStabShieldless;
						}

						local connection
						connection = Tool:GetAttributeChangedSignal("Direction"):Connect(function()
							for i, v in pairs(Anims) do
								if i == Tool:GetAttribute("Direction") then
									local direction = Tool:GetAttribute("Direction")
									directionalAnim = Anims[direction]
									
									print(direction, directionalAnim)

									for i, v in pairs(possibleAttackAnimations) do
										if v.IsPlaying then
											v:Stop()
										end
									end

									if directionalAnim then
										directionalAnim:Play()
									end
									
									local connection2
									connection2 = directionalAnim.KeyframeReached:Connect(function(kf)
										if kf == "Prepare" and canPrepare then
											directionalAnim:AdjustSpeed(0)
											connection2:Disconnect()
										end
									end)
									
									connection:Disconnect()
									break
								end
							end
						end)
					end
				end
			end
		end
	end
LocalScript function for damage (problem is here)
UIS.InputEnded:Connect(function(input)
	if character:FindFirstChild(script.Parent.Name) then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			directionalAnim:AdjustSpeed(attackSpeed[Tool.Name])
			keyFrameReached(directionalAnim) -- PROBLEM is here, it runs one more time each time InputEnded ( does x2 damage each time )
			Tool:SetAttribute("Direction", "None")
			canDirection = true
		end
	end
end)
2 Likes

Solved it myself. I made a new .KeyframeReached function for each animation, instead of calling one from InputBegan function.

1 Like