Only one player can use Server Script at a time?

I made a server script that handles the event of someone punching, however, when one player punches, it goes on cooldown for all the other players as well, meaning, when one player is punching, the other player cannot punch at the same time.

I have no idea on how to fix this.

Video below of problem being displayed:

When he is punching, I am constantly clicking the whole time to punch, but it doesn’t work. We cannot punch at the same time. I have no idea what’s going wrong.

-- Variables:
local Remote = game.ReplicatedStorage.Combat

local hit = {}

local Combo = 1

-- Settings:

local activated = false

local dmg = 5

-- Animations: 


local m1ID = "rbxassetid://7368525192"

local m1Animation = Instance.new("Animation")
m1Animation.AnimationId = m1ID

local m1evenID = "rbxassetid://7440653192"

local m1evenAnimation = Instance.new("Animation")
m1evenAnimation.AnimationId = m1evenID

local m1soundID = "rbxassetid://278061737"

local m1Sound = Instance.new("Sound")
m1Sound.SoundId = m1soundID

local m1soundMissID = "rbxassetid://138097048"

local m1soundMiss = Instance.new("Sound")
m1soundMiss.SoundId = m1soundMissID

Staggered = 1

-- Script:

Remote.OnServerEvent:Connect(function(player, cf)
	local char = player.Character
	
	--------------------------------------------------------------------------------------------------------------------------------------------------------------------
	local CombatStatus = char.Combat

	if activated == false and Combo == 1 and CombatStatus.Value ~= "Staggered" and CombatStatus.Value ~= "Blocking" then

		activated = true

		local PunchAnimation = char.Humanoid:LoadAnimation(m1Animation)
		PunchAnimation:Play()

		local hitbox = Region3.new(cf - Vector3.new(2,3,1.5), cf + Vector3.new(2,3,1.5))

		local enemy = workspace:FindPartsInRegion3(hitbox,nil,30)

		for i,v in pairs(enemy) do
			-- If the Object is a Humanoid, and isn't me, then do the following.				
			
			if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= char then
				
				-- Basically, we're making sure that anything cycling through the table, isn't cycled more than once, by putting their parent in a table.
				-- They then, can't get in if they're IN that table.
				
				if table.find (hit,v.Parent) == nil or false then		

					print ("Hit!")

					if v.Parent:FindFirstChild("Combat").Value == "Blocking" then 
						print("YOOO HE BLOCKED!")

						CombatStatus.Value = "Punching"

						local blockAnimation = Instance.new("Animation")
						blockAnimation.AnimationId = "rbxassetid://7368171308"

						local blockTrack = v.Parent.Humanoid:LoadAnimation(blockAnimation)
						blockTrack:Play()

						m1Sound.Parent = char.Humanoid
						m1Sound:Play()

					else

						CombatStatus.Value = "Punching"

						v.Parent.Humanoid:TakeDamage(dmg)

						-- Knockback
						local Knockback = Instance.new("BodyVelocity")
						Knockback.Parent = v.Parent.HumanoidRootPart

						Knockback.MaxForce = Vector3.new(25000,25000,25000)
						Knockback.P = 3
						Knockback.Velocity = char.HumanoidRootPart.CFrame.lookVector*5

						game.Debris:AddItem(Knockback,0.2)

						-- Staggering

						local staggerAnimation = Instance.new("Animation")
						staggerAnimation.AnimationId = "rbxassetid://7476542575"

						local staggerTrack = v.Parent.Humanoid:LoadAnimation(staggerAnimation)
						staggerTrack:Play()

						Staggered = 1

						if v.Parent:FindFirstChild("Combat").Value ~= "Staggered" then
							coroutine.wrap(function()


								print(v.Parent.Combat.Value)
								v.Parent:FindFirstChild("Combat").Value = "Staggered"
								v.Parent.Humanoid.WalkSpeed = 0

								repeat 
									wait(0.1)

									Staggered = Staggered - .1


								until Staggered <= 0


								v.Parent.Humanoid.WalkSpeed = v.Parent:FindFirstChild("BaseSpeed").Value
								print("opponent is no longer staggered!")
								v.Parent:FindFirstChild("Combat").Value = "None"
								print(v.Parent.Combat.Value)

							end)()
						end
					end
					table.insert(hit,v.Parent)					
				end
				-- If the Object isn't a Humanoid, then you likely missed. Play whizzing sound, still drain stamina.
			else
				m1soundMiss.Parent = char.Humanoid
				m1soundMiss:Play()
				CombatStatus.Value = "Punching"
			end

		end
		table.clear(hit)

		repeat wait() until PunchAnimation.IsPlaying == false
		print ("Strike again!")

		CombatStatus.Value = "None"

		activated = false
		
		print ("Combo | "..Combo)
		Combo = 2


		coroutine.wrap(function()
			wait (1)
			if Combo == 2 then 
				Combo = 1
				print ("A second has passed, Combo Reset to 1.")
			end
		end)()

------------------------------------------------------------------------------------------------------------------------------------------------------------------

	elseif activated == false and Combo == 2 and CombatStatus.Value ~= "Staggered" and CombatStatus.Value ~= "Blocking" then

		activated = true

		local PunchAnimation = char.Humanoid:LoadAnimation(m1evenAnimation)
		PunchAnimation:Play()

		local hitbox = Region3.new(cf - Vector3.new(2,3,1.5), cf + Vector3.new(2,3,1.5))
	
		local enemy = workspace:FindPartsInRegion3(hitbox,nil,30)

		for i,v in pairs(enemy) do
			-- If the Object is a Humanoid, and isn't me, then do the following.				

			if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= char then

				-- Basically, we're making sure that anything cycling through the table, isn't cycled more than once, by putting their parent in a table.
				-- They then, can't get in if they're IN that table.

				if table.find (hit,v.Parent) == nil or false then		

					print ("Hit!")

					if v.Parent:FindFirstChild("Combat").Value == "Blocking" then 
						print("YOOO HE BLOCKED!")

						CombatStatus.Value = "Punching"

						local blockAnimation = Instance.new("Animation")
						blockAnimation.AnimationId = "rbxassetid://7368171308"

						local blockTrack = v.Parent.Humanoid:LoadAnimation(blockAnimation)
						blockTrack:Play()

						m1Sound.Parent = char.Humanoid
						m1Sound:Play()

					else

						CombatStatus.Value = "Punching"

						v.Parent.Humanoid:TakeDamage(dmg)

						-- Knockback
						local Knockback = Instance.new("BodyVelocity")
						Knockback.Parent = v.Parent.HumanoidRootPart

						Knockback.MaxForce = Vector3.new(25000,25000,25000)
						Knockback.P = 3
						Knockback.Velocity = char.HumanoidRootPart.CFrame.lookVector*5

						game.Debris:AddItem(Knockback,0.2)

						-- Staggering

						local staggerAnimation = Instance.new("Animation")
						staggerAnimation.AnimationId = "rbxassetid://7476542575"

						local staggerTrack = v.Parent.Humanoid:LoadAnimation(staggerAnimation)
						staggerTrack:Play()

						Staggered = 1

						if v.Parent:FindFirstChild("Combat").Value ~= "Staggered" then
							coroutine.wrap(function()


								print(v.Parent.Combat.Value)
								v.Parent:FindFirstChild("Combat").Value = "Staggered"
								v.Parent.Humanoid.WalkSpeed = 0

								repeat 
									wait(0.1)

									Staggered = Staggered - .1


								until Staggered <= 0


								v.Parent.Humanoid.WalkSpeed = v.Parent:FindFirstChild("BaseSpeed").Value
								print("opponent is no longer staggered!")
								v.Parent:FindFirstChild("Combat").Value = "None"
								print(v.Parent.Combat.Value)

							end)()
						end
					end
					table.insert(hit,v.Parent)					
				end
				-- If the Object isn't a Humanoid, then you likely missed. Play whizzing sound, still drain stamina.
			else
				m1soundMiss.Parent = char.Humanoid
				m1soundMiss:Play()
				CombatStatus.Value = "Punching"
			end

		end
		table.clear(hit)

		repeat wait() until PunchAnimation.IsPlaying == false
		print ("Strike again!")

		CombatStatus.Value = "None"

		activated = false

		print ("Combo | "..Combo)
		Combo = 3


		coroutine.wrap(function()
			wait (1)
			if Combo == 3 then 
				Combo = 1
				print ("A second has passed, Combo Reset to 1.")
			end
		end)()

------------------------------------------------------------------------------------------------------------------------------------------------------------------

	elseif activated == false and Combo == 3 and CombatStatus.Value ~= "Staggered" and CombatStatus.Value ~= "Blocking" then

		activated = true

		local PunchAnimation = char.Humanoid:LoadAnimation(m1Animation)
		PunchAnimation:Play()

		local hitbox = Region3.new(cf - Vector3.new(2,3,1.5), cf + Vector3.new(2,3,1.5))

		local enemy = workspace:FindPartsInRegion3(hitbox,nil,30)

		for i,v in pairs(enemy) do
			-- If the Object is a Humanoid, and isn't me, then do the following.				

			if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= char then

				-- Basically, we're making sure that anything cycling through the table, isn't cycled more than once, by putting their parent in a table.
				-- They then, can't get in if they're IN that table.

				if table.find (hit,v.Parent) == nil or false then		

					print ("Hit!")

					if v.Parent:FindFirstChild("Combat").Value == "Blocking" then 
						print("YOOO HE BLOCKED!")

						CombatStatus.Value = "Punching"

						local blockAnimation = Instance.new("Animation")
						blockAnimation.AnimationId = "rbxassetid://7368171308"

						local blockTrack = v.Parent.Humanoid:LoadAnimation(blockAnimation)
						blockTrack:Play()

						m1Sound.Parent = char.Humanoid
						m1Sound:Play()

					else

						CombatStatus.Value = "Punching"

						v.Parent.Humanoid:TakeDamage(dmg)

						-- Knockback
						local Knockback = Instance.new("BodyVelocity")
						Knockback.Parent = v.Parent.HumanoidRootPart

						Knockback.MaxForce = Vector3.new(25000,25000,25000)
						Knockback.P = 3
						Knockback.Velocity = char.HumanoidRootPart.CFrame.lookVector*5

						game.Debris:AddItem(Knockback,0.2)

						-- Staggering


						local staggerAnimation = Instance.new("Animation")
						staggerAnimation.AnimationId = "rbxassetid://7476542575"

						local staggerTrack = v.Parent.Humanoid:LoadAnimation(staggerAnimation)
						staggerTrack:Play()

						Staggered = 1

						if v.Parent:FindFirstChild("Combat").Value ~= "Staggered" then
							coroutine.wrap(function()


								print(v.Parent.Combat.Value)
								v.Parent:FindFirstChild("Combat").Value = "Staggered"
								v.Parent.Humanoid.WalkSpeed = 0

								repeat 
									wait(0.1)

									Staggered = Staggered - .1


								until Staggered <= 0


								v.Parent.Humanoid.WalkSpeed = v.Parent:FindFirstChild("BaseSpeed").Value
								print("opponent is no longer staggered!")
								v.Parent:FindFirstChild("Combat").Value = "None"
								print(v.Parent.Combat.Value)

							end)()
						end
					end
					table.insert(hit,v.Parent)					
				end
				-- If the Object isn't a Humanoid, then you likely missed. Play whizzing sound, still drain stamina.
			else
				m1soundMiss.Parent = char.Humanoid
				m1soundMiss:Play()
				CombatStatus.Value = "Punching"
			end

		end
		table.clear(hit)

		repeat wait() until PunchAnimation.IsPlaying == false
		print ("Strike again!")

		CombatStatus.Value = "None"

		activated = false

		print ("Combo | "..Combo)
		Combo = 4


		coroutine.wrap(function()
			wait (1)
			if Combo == 4 then 
				Combo = 1
				print ("A second has passed, Combo Reset to 1.")
			end
		end)()

------------------------------------------------------------------------------------------------------------------------------------------------------------------

	elseif activated == false and Combo == 4 and CombatStatus.Value ~= "Staggered" and CombatStatus.Value ~= "Blocking" then

		activated = true

		local PunchAnimation = char.Humanoid:LoadAnimation(m1evenAnimation)
		PunchAnimation:Play()

		local hitbox = Region3.new(cf - Vector3.new(2,3,1.5), cf + Vector3.new(2,3,1.5))

		local enemy = workspace:FindPartsInRegion3(hitbox,nil,30)

		for i,v in pairs(enemy) do
			-- If the Object is a Humanoid, and isn't me, then do the following.				

			if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= char then

				-- Basically, we're making sure that anything cycling through the table, isn't cycled more than once, by putting their parent in a table.
				-- They then, can't get in if they're IN that table.

				if table.find (hit,v.Parent) == nil or false then		

					print ("Hit!")

					if v.Parent:FindFirstChild("Combat").Value == "Blocking" then 
						print("YOOO HE BLOCKED!")

						CombatStatus.Value = "Punching"

						local blockAnimation = Instance.new("Animation")
						blockAnimation.AnimationId = "rbxassetid://7368171308"

						local blockTrack = v.Parent.Humanoid:LoadAnimation(blockAnimation)
						blockTrack:Play()

						m1Sound.Parent = char.Humanoid
						m1Sound:Play()

					else

						CombatStatus.Value = "Punching"

						v.Parent.Humanoid:TakeDamage(dmg)

						-- Knockback
						local Knockback = Instance.new("BodyVelocity")
						Knockback.Parent = v.Parent.HumanoidRootPart

						Knockback.MaxForce = Vector3.new(25000,25000,25000)
						Knockback.P = 3
						Knockback.Velocity = char.HumanoidRootPart.CFrame.lookVector*15

						game.Debris:AddItem(Knockback,1.5)

						-- Staggering


						local staggerAnimation = Instance.new("Animation")
						staggerAnimation.AnimationId = "rbxassetid://7476542575"

						local staggerTrack = v.Parent.Humanoid:LoadAnimation(staggerAnimation)
						staggerTrack:Play()

						Staggered = .5

						if v.Parent:FindFirstChild("Combat").Value ~= "Staggered" then
							coroutine.wrap(function()


								print(v.Parent.Combat.Value)
								v.Parent:FindFirstChild("Combat").Value = "Staggered"
								v.Parent.Humanoid.WalkSpeed = 0

								repeat 
									wait(0.1)

									Staggered = Staggered - .1


								until Staggered <= 0


								v.Parent.Humanoid.WalkSpeed = v.Parent:FindFirstChild("BaseSpeed").Value
								print("opponent is no longer staggered!")
								v.Parent:FindFirstChild("Combat").Value = "None"
								print(v.Parent.Combat.Value)

							end)()
						end
					end
					table.insert(hit,v.Parent)					
				end
				-- If the Object isn't a Humanoid, then you likely missed. Play whizzing sound, still drain stamina.
			else
				m1soundMiss.Parent = char.Humanoid
				m1soundMiss:Play()
				CombatStatus.Value = "Punching"
			end

		end
		table.clear(hit)

		repeat wait() until PunchAnimation.IsPlaying == false
		print ("Strike again!")

		CombatStatus.Value = "None"

		print ("Combo | "..Combo)
		Combo = 1


		coroutine.wrap(function()
			wait (1)
			activated = false
		end)()
	end
end)
1 Like

Hm,I believe,the cooldown is for all players,best to have the variable under a line or under a function,might solve the problem,I’m a bit busy right now but I just wanted to help you. (Will be back if this continues! Good luck!)

How is the script supposed to still work if it’s heavily dependent on those variables being outside of the function?

Or, how could I make it so the script doesn’t depend on the variables outside of the function?

1 Like

I would suggest using a table whose index is the player and the value is the cooldown.

1 Like

local cooldown = {}
cooldown[bob] = false
etc…

 -- //  Creating the Debounce 
local Debounce = {}
Debounce[Player.Name] = {
 Debounce = false
}
-- //  Checking
if not Debounce[Player.Name].Debounce then
      -- // Setting it to true
      Debounce[Player.Name].Debounce = true
       -- // Punch Code goes here 
       -- Code
       -- // Waiting until it goes false then repeats the debounce
       task.wait(1)
       -- // Setting it to false
       Debounce[Player.Name].Debounce = false
end
2 Likes

That worked perfectly when I implemented it, thanks!

2 Likes

the cooldown no longer works for me when i implemented this