Animations resetting over and over again when running into a wall

so I have this script that lets the player run while holding shift and also has a function so when they stop while holding shift they don’t run in place, now a problem is when a player runs into a wall the animation just plays and resets over and over again until they get away from the wall or stop holding shift so i want to know if there is a way for me to detect when they run into a wall and when they stop by their own will.

local UserInputService = game:GetService("UserInputService")
--
local RS = game:GetService("ReplicatedStorage")
local RunFolder = RS.RunEvents
local RunStart = RunFolder.RunStart
local RunStop = RunFolder.RunStop
--

local Animation = script:WaitForChild("SprintAnimation")
local PLRS = game:GetService("Players").LocalPlayer
local Chr = PLRS.Character or PLRS.CharacterAdded:Wait()
local humanoid = Chr:WaitForChild("Humanoid")
local AnimTrack = humanoid:LoadAnimation(Animation)
local anmplaying = true
local Animation2 = script.WalkAnimation
local AnimTrack2 = humanoid:LoadAnimation(Animation2)
local IsRunning = false
local isCrouching = script.Parent:WaitForChild("AnimationValuesFolder").IsCrouching
local isProning = script.Parent:WaitForChild("AnimationValuesFolder").IsProning
local RemoteEventProne = game.ReplicatedStorage.AnimationEvents.IsProne
local RemoteEventNoProne = game.ReplicatedStorage.AnimationEvents.IsProneNot

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

-----------------------------------------------------------------------------------
UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if isProning.Value == false then
		if isCrouching.Value == false then
		local plr = game.Players.LocalPlayer.Character.Humanoid
		if plr.MoveDirection.Magnitude > 0 then
		IsRunning = true
		plr.WalkSpeed = 22
		local DefaultFoV2 = 70
		local Properties = {FieldOfView = DefaultFoV2 + 15}
		local Info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- edit as you want
		local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, Info, Properties)
		T:Play()
			
			AnimTrack:Play(0.25)
			end
		end
		end
	end
end)
-----------------------------------------------------------------------------------
UserInputService.InputEnded:Connect(function(input,gameProccesedEvent)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if isCrouching.Value == false then
			if isProning.Value == false then
		local plr = game.Players.LocalPlayer.Character.Humanoid
		
		IsRunning = false
				plr.WalkSpeed = 12
		local DefaultFoV2 = 85
		local Properties = {FieldOfView = DefaultFoV2 - 15}
		local Info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- edit as you want
		local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, Info, Properties)
		T:Play()
		
		AnimTrack:Stop(0.25)
			end
		end
	end
end)

humanoid.Running:Connect(function(speed)
	if  IsRunning == true then
		if speed > 0 then -- if is moving then
			
			local plr = game.Players.LocalPlayer.Character.Humanoid
			AnimTrack:Play(0.25)
			plr.WalkSpeed = 22
			local DefaultFoV2 = 70
			local Properties = {FieldOfView = DefaultFoV2 + 15}
			local Info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- edit as you want
			local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, Info, Properties)
			T:Play()

		else -- if is no longer moving then
			
			local plr = game.Players.LocalPlayer.Character.Humanoid
			AnimTrack:Stop(0.25)
			plr.WalkSpeed = 12
			local DefaultFoV2 = 85
			local Properties = {FieldOfView = DefaultFoV2 - 15}
			local Info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- edit as you want
			local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, Info, Properties)
			T:Play()
			
			end
		end
end)

Example:
https://gyazo.com/61723d98ac0dc361f29dae0a09841b52

2 Likes

Bump cus I am desperate and this problem is a very big one

This is a tricky problem to solve, but this is a solution I implement sometimes. What you do is get the speed of the player within a range to check if they are at the correct running speed. In the video below, at regular speed I reach around 18-19 speed. Then hitting a wall, I reach 0 and if I wiggle around the wall I reach around 8-10. So what you could do is check within a range, and if they are not at a specific speed level (hitting a wall), stop the animation.

The code from the video is relatively simple, this is all I did to get the player speed:

		RunService.Heartbeat:Connect(function(step)
			print(player.Character.HumanoidRootPart.Velocity.magnitude)
		end)

think you could give a example on how i would detect the speed and change an animation with it? so i could both stop and start the animation?

Like an if statement?

This post might help you:

(just do the opposite)

nevermind ill try something myself and keep you updated

1 Like

problem is i already use .running to detect if the player stops while holding shift so i think that would be a problem

yeah i really do not understand what you meant, how can i use what you showed me to stop the wall glitching? sorry to bother but this is a big problem in the game

Basically what is happening is the character is switching into running, hitting the wall, then switch out of running (because of the wall).


What you can do is keep track of two sets of states: isRunning and isFastEnough
If both the states are true, play the running animation. If either are false, stop the running animation.

You can check and possibly set the animation when isRunning changes and when isFastEnough changes.

You can check the speed in a Heartbeat connection like DataSigh used. You can update running with your InputEnded/InputBegan events.


Another solution (I believe this is what the default animation scripts do): you can set the animation speed based on the character’s speed.


Another solution, you can switch out the default walk/run animation when the character starts sprinting (functions similar to the proposed solution above).


Edit:
Here is some general code:

local START_RUN_SPEED = 6.0

-- code to manage isRunningKey variable

local isRunningKey -- if running key down
local isFastEnough -- if moving fast enough to start running
local isRunning -- if running

local RunService = game:GetService("RunService")

local function endRun()
	isRunning = false
	-- code to end running
end

local function startRun()
	isRunning = true
	-- code to start running (FoV, speed, etc)
end

RunService.Heartbeat:Connect(function()
	local character = game.Players.LocalPlayer.Character
	local humanoidRootPart = character.HumanoidRootPart
	local humanoid = character.Humanoid

	local velocity = humanoidRootPart.Velocity
	local speed = (velocity.x*velocity.x + velocity.z*velocity.z)^(0.5)
	
	if isRunning then
		if (speed < START_RUN_SPEED) then
			isFastEnough = false
			endRun()
		else if (not isRunningKey) then
			endRun()
		end
	else
		if (speed >= START_RUN_SPEED) then
			isFastEnough = true
			if isRunningKey then
				startRun()
			end
		else
			isFastEnough = false
		end
	end
end)
1 Like

Thanks, ill try this and get back to you soon, taking a mini-break at the moment so it may take time to update you guys.

1 Like

thanks, man you do not know how big this was for us, thank you very much.

1 Like