Make the opponent stunned in a combo

So I’m having trouble with my melee combat. I want to achieve something like this:

The yellow bar shown in the video represents the amount of time the opponent is stunned. If the player stop hitting, it resets.

But if the player still continue to hit the opponent, he’ll get stunned til the player stop combo-ing:

My script wont change the stunned value unless the player clicks again. Here’s my script:

	local stunned = false
	local current = 0
	local previous = 0

	mouse.Button1Down:Connect(function()

		if db == false and game.Players.LocalPlayer.Data.Combat.Value == true and plr.Character.Block.Value == false then
			db = true
			current = tick()
			local passed = current - previous
			if passed < 1 then
				combo = combo + 1
				if stunned == false then
					stunned = true
				end

				if combo > 5 then
					wait(3)
					db = false
					combo = 1
				end
			else
				stunned = false
				combo = 1
			end
			game.ReplicatedStorage.Combat.Melee:FireServer(combo, stunned)
		end
	end)

I’ve tried to use os.time(). The problem remains the same. If you’re confused about what I am talking about, please leave a comment; I’ll happily reply to you. I’ve been having the same problem for so long. Please help.

1 Like

Hey!! I’m sorry, but I’m not 100% sure what you need help with. Would you be able to explain a bit more?

Just a thing, for your if then statement, I would recommend putting it into separate lines (Examples below)

Disclaimer I did not test this script at all, and I may have missed an end.

local stunned = false
	local current = 0
	local previous = 0

	mouse.Button1Down:Connect(function()

		if db == false then
          local player = game.Players.LocalPlayer

               if player.Data.Combat.Value == true then
                     if player.Character.Block.Value == false then
db = true
			current = tick()
			local passed = current - previous
			if passed < 1 then
				combo = combo + 1
				if stunned == false then
					stunned = true
				end

				if combo > 5 then
					wait(3)
					db = false
					combo = 1
				end
			else
				stunned = false
				combo = 1
			end
			game.ReplicatedStorage.Combat.Melee:FireServer(combo, stunned)
               else 
                      warn("Error")
       else
              warn("Error") -- The warn will show up orange, so it's easier to see.

		end
	end)
3 Likes

The essence of stun is stopping the player from performing actions such as walking, blocking, and combat. Usually whenever the player attacks someone you create a object inside their character called “Stun” that gets deleted in x seconds, and they check if that object exists in events like InputBegan or RemoteEvents to stop actions.

To stop movement you can disable/enable the controls using ChildAdded and ChildRemoved on the character then checking if the the returned parameter is the stun object

Anyways, I highly recommend scripting a State module for all your needs, it’s much easier to mess with

4 Likes