Disable running while blocking not working

I have a pretty good idea on how to get it done but I have no clue why its not working. So basically the goal it stop the user from running if the starts blocking. I apply an attribute to the character to identify when the user is blocking. I then check the attribute and try to stop the running state if the attribute is true. But again, it’s not working. Can I get some insight on how to fix this issue? Thanks!

Video example of the issue : Watch 2024-03-25 19-56-13 | Streamable

This is the running scrip

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local runSpeed = 30
local walkSpeed = 16

local forwardKey = Enum.KeyCode.W
local backwardKey = Enum.KeyCode.S

local animationsFolder = script
local runAnimation = animationsFolder:WaitForChild("Run")
local Data = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("Data")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:FindFirstChildOfClass("Humanoid")

local runAnimationTrack = humanoid:LoadAnimation(runAnimation)
runAnimationTrack.Priority = Enum.AnimationPriority.Action4

local isMovingForward = false

local function updateRunState()
	local isForwardKeyPressed = false
	local isBackwardKeyPressed = false
	
	for _, input in ipairs(UserInputService:GetKeysPressed()) do
		if input.KeyCode == forwardKey then
			isForwardKeyPressed = true
		elseif input.KeyCode == backwardKey then
			isBackwardKeyPressed = true
		end
	end

	local shouldRun = isForwardKeyPressed and not isBackwardKeyPressed and not player:GetAttribute("IsBlocking")

	if shouldRun then
		humanoid.WalkSpeed = runSpeed
		if not runAnimationTrack.IsPlaying then
			runAnimationTrack:Play()
		end
		character:SetAttribute("IsRunning", true)
	else
		humanoid.WalkSpeed = walkSpeed
		if runAnimationTrack.IsPlaying then
			runAnimationTrack:Stop()
		end
		character:SetAttribute("IsRunning", false)
	end
end

local function initCharacter()
	character = player.Character or player.CharacterAdded:Wait()
	humanoid = character:FindFirstChildOfClass("Humanoid")
	runAnimationTrack = humanoid:LoadAnimation(runAnimation)

	character:SetAttribute("IsRunning", false)
	character:SetAttribute("IsPunching", false)

	RunService.Heartbeat:Connect(updateRunState)

	UserInputService.InputBegan:Connect(function(input, gameProcessed)
		if not gameProcessed then
			isMovingForward = input.KeyCode == forwardKey
		end
	end)

	UserInputService.InputEnded:Connect(function(input, gameProcessed)
		if input.KeyCode == forwardKey then
			isMovingForward = false
		end
	end)
end

player.CharacterAdded:Connect(initCharacter)
initCharacter()

Data.OnClientEvent:Connect(function(data)
	if data.action == "updateRunningState" or data.action == "StopRunning" then
		isMovingForward = false
		updateRunState()
	end
end)

This is in my main combat script it’s how I enable and disable the blocking

function Block(Bool)
	local isBlocking = Bool
	print(isBlocking)
	isUserBlockingConnection:Fire(isBlocking)

	local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, not isBlocking)
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode.F then
		Block(true)
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		Block(false)
	end
end)

for setting the attribute it’s this here in a module script called BlockHandler

local BlockHandler = {}

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BridgeNet = require(ReplicatedStorage.Modules.BridgeNet2)
local SpeedManager = require(script.Parent.SpeedManager)
local Animations = ReplicatedStorage:WaitForChild("Animations")
local blockAnimation = Animations:WaitForChild("Fist"):WaitForChild("Blocking")

local isUserBlockingConnection = BridgeNet.ServerBridge("UserBlocking")

local playerAnimationTracks = {}

isUserBlockingConnection:Connect(function(player, isBlocking)
	local character = player.Character or player.CharacterAdded:Wait()

	if character then
		local isDashing = character:GetAttribute("IsDashing")
		local isPunching = character:GetAttribute("IsPunching")

		if isDashing or isPunching then
			return
		end

		character:SetAttribute("IsBlocking", isBlocking)

		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if not humanoid then return end

		local animator = humanoid:FindFirstChildOfClass("Animator") or Instance.new("Animator", humanoid)

		if not playerAnimationTracks[player] then
			playerAnimationTracks[player] = animator:LoadAnimation(blockAnimation)
		end

		local animationTrack = playerAnimationTracks[player]

		if isBlocking then
			animationTrack:Play()
			SpeedManager:ApplyEffect(character, "apply")
		else
			animationTrack:Stop()
			SpeedManager:ApplyEffect(character, "remove")
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	playerAnimationTracks[player] = nil
end)

return BlockHandler
1 Like