Blocking Animation Bug

Hey, basically, I’m trying to make a Blocking Animation with a cooldown so people can’t just spam it. That was leading to Remote Fatigue. Everything seems to work fine, until suddenly the blocking animations remains glued to the character, as if it never stopped. Pressing F again will not fix the issue.

Blocking Bug

I’ve tried many things, such as changing the UserInputService input to a key press rather than checking to see if its held by :IsKeyDown() is true or false. I’ve looked on the Developer Hub, thinking that the problem might lie in something I’m doing wrong with InputEnded

LOCAL SCRIPT

-- Variables --
local Player = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local Remote = game.ReplicatedStorage.HpBlock
local Blocking = false
local OnCooldown = false

-- Animations --

local BlockingTrack = Instance.new("Animation")
BlockingTrack.AnimationId = "rbxassetid://7368182945"
 

-- Script --
UserInputService.InputBegan:Connect(function(input,gameprocessed)
	if gameprocessed == false then
	
	-- Animation Creation --
	local Char = Player.Character
	BlockingAnimation = Char.Humanoid:LoadAnimation(BlockingTrack)

		if input.KeyCode == Enum.KeyCode.R and Blocking == false and OnCooldown ~= true then
			
			print ("I'm Blocking, young blood!")
			Remote:FireServer()
			BlockingAnimation:Play()
			print ("Fired to server!")

			Blocking = true
 
		end

	else end
end)

UserInputService.InputEnded:Connect(function(input,gameprocessed)
	if gameprocessed == false then

		if input.KeyCode == Enum.KeyCode.R and Blocking == true and OnCooldown ~= true then
			OnCooldown = true
			Blocking = false
			
			print ("I'm done blocking!")
			Remote:FireServer()
			BlockingAnimation:Stop()
			print ("I'VE PUT MY HANDS DOWN!")

			OnCooldown = true
			wait (0.5)
			OnCooldown = false
			

		end

	else end
end)

SERVER SCRIPT (Don’t think it’s needed, but just in case)

-- Variables --
local Remote = game.ReplicatedStorage.HpBlock


-- Settings --
local Blocking = false
local OnCooldown = false

-- Script --
Remote.OnServerEvent:Connect(function(Player)
local Char = Player.Character

	if Blocking == false and OnCooldown == false then
		
		local BlockingStatus = Instance.new("BoolValue")
		BlockingStatus.Name = "BlockingStatus"
		BlockingStatus.Parent = Player.Character
		BlockingStatus.Value = true

		Blocking = true

	elseif Blocking == true and OnCooldown == false then
		OnCooldown = true
		Blocking = false
		Char.BlockingStatus:Destroy()

		wait (0.5)
		OnCooldown = false
	end

end)

If anyone has a solution to this bug that I’m just not seeing, or can teach me a way to achieve my goal with a different system, could you please help? :pray: (Struggling rn)

If there’s any additional information that I can give, let me know and I will do so

2 Likes

Try doing everything on the server. Then have a “CombatState” value under the player or whatever that changes based on what the server does to your “Fighter”. That way the client can check to see what state they’re in currently to keep from firing the server if the current state can’t be overridden by the keycode. This is how I setup my combat system anyway.

Ex.

-- client
local plr = game.Players.LocalPlayer
local combatState = plr:WaitForChild("CombatState")
local combatStateChange = game.ReplicatedStorage:WaitForChidl("CombatStateChange")

inputBegan:Connect(function(i, p)
 if p then return end

 if i.KeyCode == Enum.KeyCode.F then
  if combatState.Value ~= "Idle" then return end
   
   combatStateChange:FireServer("StartBlock")
  end
 end
end)

-- server
local combatStateChange = game.ReplicatedStorage.CombatStateChange

combatStateChange.OnServerEvent:Connect(function(plr, combatState)
 if combatState == "StartBlock" then
  if plr.CombatState.Value ~= "Idle" then return end

  plr.CombatState.Value = "StartBlock"
  blockAnimation:Play()
 end
end)
1 Like

So move all of the animations and stuff to the server? . . and check to see if the ‘Blocking’ variable is in the humanoid before firing the remote?

Would that fix the issue with the animations though? . . Isn’t it the way that I’m doing the animations? My problem is not really on the server, it’s local, is the animation bug happening due to remote fatigue and such?

I will try this, just have a few questions

Sorry, I don’t think I was being clear enough, the problem wasn’t Remote Fatigue, but something that occurred when I tried to correct it, a bug with my animations where it’d hold permanently

The system I suggest:

  1. client input

  2. client check to see if they can change to that “keycode’s state” by looking at their current combat state value (or like you said, have a value inside their character) – this check is not necessary, but recommended

  3. tell server we want to change to that combat state

  4. server runs the same check that client did

  5. server also runs cooldown checks / other checks to make sure they can / it’s a legit call (not exploiter)

  6. server plays animation for that state or stops animation and sets their current state to that state

this system could also work with attacking etc.


I modified your script a bit, and this seems to be working fine.

-- Variables --
local Player = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local Blocking = false
local OnCooldown = false

-- Animations --

local BlockingTrack = Instance.new("Animation")
BlockingTrack.AnimationId = "rbxassetid://7368182945"


-- Script --
UserInputService.InputBegan:Connect(function(input,gameprocessed)
	if gameprocessed == false then
		
		-- Animation Creation --
		local Char = Player.Character
		BlockingAnimation = Char.Humanoid:LoadAnimation(BlockingTrack)

		if input.KeyCode == Enum.KeyCode.R and Blocking == false and OnCooldown ~= true then
			Blocking = true

			print ("I'm Blocking, young blood!")
			BlockingAnimation:Play()
		end
	end
end)

UserInputService.InputEnded:Connect(function(input,gameprocessed)
	if gameprocessed == false then
		
		if input.KeyCode == Enum.KeyCode.R and Blocking == true and OnCooldown ~= true then
			OnCooldown = true
			
			task.delay(.5, function() -- this will not yield the script so blocking can stop instantly, but cooldown also starts instantly
				OnCooldown = false
			end)
			
			Blocking = false

			print ("I'm done blocking!")
			BlockingAnimation:Stop()
			print ("I'VE PUT MY HANDS DOWN!")
		end
	end
end)

Note: your remote wasn’t doing anything at the moment so I just took it out.
Edit: Woops, I didn’t realise you were using it to put in a “Blocking Value”, I just saw you creating a cooldown on the server which really wouldn’t do anything if you have one on the client.

I suggest instead of starting a cooldown on the server, have the client send a string called “StartBlock” or “StopBlock” with which the server can respond accordingly.

1 Like

Video

I tested the script you gave, and it still had the same error after I spammed it enough, I don’t know what it is, I’ll be using Task Delay, and confirming the humanoid’s ‘state’ from the client before firing the remote, but it still hasn’t seemed to fix the issue

I think it has something to do with InputBegan and InputEnded, but I’m not sure

Is it printing the “I’m done blocking” when it gets stuck on the block animation?

I don’t have your animation, but just copying and pasting that code into a local script prints fine for me.

1 Like

Yes, it’s still printing it, it still fires to the server, prints that, and can even ‘continue’ to be used to block, but it’s as if the animation is suddenly permanently there after that. Like the Animation isn’t stopped. I’ve put printing before and after that to try and fix it or to find the error, but I don’t know why, the script runs but the animation doesn’t stop

With yours, it never fires to the server because there’s no remote but it prints

Ah alright. Well I noticed you using Humanoid:LoadAnimation() which is deprecated. Try instead using Humanoid.Animator:LoadAnimation()

1 Like

It works exactly the same, eventually getting the same permanent block hold. I just wish the output at least showed some sort of error, but it shows nothing

https://gyazo.com/f2c2a4564aa924de2ec9fd7ea7e4f0c1

Can you send me the exact code your using for client and server? I’ll just replace the animation with something else

Ah I gtg now. I won’t be back for a while, lmk if you figure it out before I get back. If not, I’ll test then

1 Like

My server script and local script are at the first post, they don’t have the task delay but I can add that if need be.

And thank you for being so patient, I know the problem keeps popping up, and you can’t even see it yourself. I will let you know if its fixed.

:+1:

. . . I hate Roblox sometimes, it was the most simple error. I just had the ‘LoadAnimation’ outside of the ifstatement.

:man_facepalming:

I’ll use what you’ve taught me, Thank you man.

1 Like