Punch click or hold don't work together

so i have a combo system which plays punch animations one after the another with clicking after the last one ends
code:

-- mouse click version
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if not isPlaying then
		
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			-- for every punch in punchanimations
			isPlaying = true
			if currentPunchPosition < 5 then
				currentTrack = punchAnimationTracks[currentPunchPosition]
				currentTrack:Play()

				-- Wait until the current animation finishes
				currentTrack.Stopped:Wait()

				print("currentposition:", currentPunchPosition, "currenttrack:", currentTrack, ": for mouse click combo")

				-- Move to the next punch in the combo
				currentPunchPosition += 1
			else
				-- Reset punch combo if it exceeds the limit
				currentPunchPosition = 1
			end

			-- Activate the hitbox for 0.1 seconds
			task.spawn(function()
				hitboxPartClone.Parent = workspace
				task.wait(0.1)
				hitboxPartClone.Parent = nil
			end)

			-- Now that the punch has completed, allow the next input
			isPlaying = false
		end
	end

end)

-- mouse hold version
task.spawn(function() 
	print(mousePressed)
	while true do
		task.wait(1)
		if mousePressed and not isPlaying then
			isPlaying = true


			if currentPunchPosition < 5 then

				print(currentPunchPosition,": for Hold combo")

				currentTrack = punchAnimationTracks[currentPunchPosition]
				currentTrack:Play()
				currentTrack.Stopped:Wait()
				currentPunchPosition += 1

				task.spawn(function()
					hitboxPartClone.Parent = workspace
					task.wait(0.1)
					hitboxPartClone.Parent = nil
				end)




			else
				currentPunchPosition = 1

			end
		end
		isPlaying = false
	end
end)

the problem is that the mouse click and the mouse hold dont work together:

  1. it plays the 2 animations from both the mouse click and mouse hold version
  2. sometimes the mouse hold version is stuck in the animations, probably because of the isPlaying debounce.
1 Like

It’s because of the fact that both the mouse click and mouse hold versions share the same animation logic (isPlaying, currentPunchPosition, etc.), that leads to the conflict when they are run simultaneously, for you to fix this you need to seperate the logic for mouse click and mouse hold to ensure they dont interfere with each other

this is for the mouse click version

local isPlayingClick = false  -- Separate flag for mouse click

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if not isPlayingClick and not mousePressed then  -- Check if mouse hold is not active
		
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			isPlayingClick = true

			if currentPunchPosition < 5 then
				currentTrack = punchAnimationTracks[currentPunchPosition]
				currentTrack:Play()

				-- Wait until the current animation finishes
				currentTrack.Stopped:Wait()

				print("currentposition:", currentPunchPosition, "currenttrack:", currentTrack, ": for mouse click combo")

				-- Move to the next punch in the combo
				currentPunchPosition += 1
			else
				-- Reset punch combo if it exceeds the limit
				currentPunchPosition = 1
			end

			-- Activate the hitbox for 0.1 seconds
			task.spawn(function()
				hitboxPartClone.Parent = workspace
				task.wait(0.1)
				hitboxPartClone.Parent = nil
			end)

			isPlayingClick = false
		end
	end
end)

and this is for the mouse hold version

local isPlayingHold = false  -- Separate flag for mouse hold

task.spawn(function() 
	print(mousePressed)
	while true do
		task.wait(1)
		if mousePressed and not isPlayingHold and not isPlayingClick then  -- Ensure no click is happening
			isPlayingHold = true

			if currentPunchPosition < 5 then
				print(currentPunchPosition, ": for Hold combo")

				currentTrack = punchAnimationTracks[currentPunchPosition]
				currentTrack:Play()

				-- Wait until the current animation finishes
				currentTrack.Stopped:Wait()

				currentPunchPosition += 1

				-- Activate the hitbox for 0.1 seconds
				task.spawn(function()
					hitboxPartClone.Parent = workspace
					task.wait(0.1)
					hitboxPartClone.Parent = nil
				end)

			else
				currentPunchPosition = 1
			end

			isPlayingHold = false
		end
	end
end)

this separates the isplaying flags to ensure that each input type doesn’t interfere with the other and gives it a proper animation flow to where it handles the Stopped:Wait() event correctly so that one version doesn’t change the isPlaying and this avoids the animations getting stuck

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.