Animation repeating itself after running into a wall

I have a problem where the run or walk animation constantly repeats itself when running into a wall.


This is what happens with the default animate script.


And this is what happens with mine.
Does someone have any suggestions how Icould fix that or how roblox prevents that from happening?

Edit: this is the script

wait(1)
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:FindFirstChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local footsteps = require(script:WaitForChild("Footsteps"))
local HRP = character:WaitForChild("HumanoidRootPart")

local lastFootstepSound = nil
local canJump = false

local KeyDetection = require(script.KeyDetection)
KeyDetection:Start()

local lastPressedTime = nil

local animationTrackStorage = {}

local animTracks = {
	Walk = script:WaitForChild("Walk"),
	Run = script:WaitForChild("Run"),
	Jump = script:WaitForChild("Jump"),
	Idle = script:WaitForChild("Idle"),
	Swim = script:WaitForChild("Swim"),
	Climb = script:WaitForChild("Climb"),
	Died = script:WaitForChild("Died")
}

for Name, Object in pairs(animTracks) do
	animTracks[Name] = animator:LoadAnimation(Object)
end

function StopAnims(ignoreAnim)
	for i, animTrack in pairs(animator:GetPlayingAnimationTracks()) do
		if animTrack.Name == ignoreAnim or ignoreAnim == "Run" and animTracks.Name == "Walk"  then
			continue
		end
		animTrack:Stop()
	end
end

function hitAWall(playerSpeed)
	RS.Heartbeat:Connect(function(step)
		playerSpeed = player.Character.HumanoidRootPart.Velocity.magnitude

		if playerSpeed < 3 and (animTracks["Walk"].isPlaying or animTracks["Run"].isPlaying) then
			if animTracks["Walk"].isPlaying then
				animTracks["Walk"]:Stop()
				animTracks["Idle"]:Play()
			elseif animTracks["Run"].isPlaying then
				animTracks["Run"]:Stop()
				animTracks["Idle"]:Play()
			end
		end
	end)
end

function animStoppedDetection()
	animTracks["Walk"].Stopped:Connect(function()
		local walkAnimEnd = animTracks["Walk"].TimePosition
		animationTrackStorage["Walk"] = walkAnimEnd
		print("Walk animation stopped at:", walkAnimEnd)
	end)

	animTracks["Run"].Stopped:Connect(function()
		local runAnimEnd = animTracks["Run"].TimePosition
		animationTrackStorage["Run"] = runAnimEnd
		print("Run animation stopped at:", runAnimEnd)
	end)
end

local function onTouched(hit)
	if hit:IsA("BasePart") and not hit:IsDescendantOf(character) and animTracks["Walk"].isPlaying then
		local walkAnimEnd = animTracks["Walk"].TimePosition
		animationTrackStorage["Walk"] = walkAnimEnd
	elseif hit:IsA("BasePart") and not hit:IsDescendantOf(character) and animTracks["Run"].isPlaying then
		local runAnimEnd = animTracks["Run"].TimePosition
		animationTrackStorage["Run"] = runAnimEnd
	end
end

local function Movement()
	if humanoid.FloorMaterial ~= Enum.Material.Air or humanoid.FloorMaterial ~= Enum.Material.Water then
		if animTracks["Run"].isPlaying then
			animTracks["Walk"]:Stop()
		elseif animTracks["Walk"].isPlaying then
			animTracks["Run"]:Stop()

		end

		if humanoid.WalkSpeed <= 9 and humanoid.MoveDirection.Magnitude > 0 then
			animTracks["Run"]:Stop()
			animTracks["Walk"]:Play()

			if KeyDetection.pressed > 1 then
				local currentTime = tick()

				if lastPressedTime == nil then
					lastPressedTime = currentTime
				elseif currentTime - lastPressedTime < 0.5 then
					animTracks["Walk"].TimePosition = animationTrackStorage.Walk or 0
				elseif currentTime - lastPressedTime > 0.5 then	
					animTracks["Walk"].TimePosition = 0
				end

				lastPressedTime = currentTime
			end
		elseif humanoid.WalkSpeed > 9 and humanoid.MoveDirection.Magnitude > 0 then
			animTracks["Walk"]:Stop()
			animTracks["Run"]:Play()

			if KeyDetection.pressed > 1 then
				local currentTime = tick()

				if lastPressedTime == nil then
					lastPressedTime = currentTime
				elseif currentTime - lastPressedTime < 0.5 then
					animTracks["Run"].TimePosition = animationTrackStorage.Run or 0
				elseif currentTime - lastPressedTime > 0.5 then	
					animTracks["Run"].TimePosition = 0
				end

				lastPressedTime = currentTime
			end
		elseif humanoid.MoveDirection.Magnitude == 0 then
			animTracks["Walk"]:Stop()
			animTracks["Run"]:Stop()
			animTracks["Idle"]:Play()
		end
	end
end

local function PlayFootstepSound(foot, material)
	if not foot then return end

	local sounds = footsteps.sounds[material]
	if not sounds then return end

	local random = Random.new()
	local soundId = sounds[random:NextInteger(1, #sounds)]

	if soundId and soundId ~= lastFootstepSound then
		lastFootstepSound = soundId
		local sfx = Instance.new("Sound")
		sfx.SoundId = soundId
		sfx.RollOffMaxDistance = 100
		sfx.RollOffMinDistance = 10
		sfx.Volume = footsteps.volume[material] or 0.5
		sfx.Parent = foot
		sfx:Play()
		task.spawn(function()
			sfx.Ended:Wait()
			sfx:Destroy()
		end)
	else
		PlayFootstepSound(foot, material)
	end
end

local function OnFootStep(side)
	local leg = character:FindFirstChild(side.."Foot")
	local foot = leg:FindFirstChild(side.."FootAttachment")
	local floorMaterial = humanoid.FloorMaterial
	local material = footsteps.materialMap[floorMaterial]

	PlayFootstepSound(foot, material)
end

function Jump()
	animTracks["Jump"]:Play()
end

function Swim()
	animTracks["Swim"]:Play()
end

function Climb()
	animTracks["Climb"]:Play()
end

function Died()
	animTracks["Died"]:Play()
end

function AnimationLoader()
	animTracks["Walk"].Priority = Enum.AnimationPriority.Movement
	animTracks["Run"].Priority = Enum.AnimationPriority.Movement
	animTracks["Jump"].Priority = Enum.AnimationPriority.Action
	animTracks["Idle"].Priority = Enum.AnimationPriority.Idle
	
	animTracks["Walk"].Looped = false
	animTracks["Run"].Looped = false

	humanoid.Running:Connect(Movement)	
	humanoid.Jumping:Connect(Jump)
	humanoid.Swimming:Connect(Swim)
	humanoid.Climbing:Connect(Climb)
	humanoid.Died:Connect(Died)

	animTracks["Idle"]:Play()

	animTracks["Walk"]:GetMarkerReachedSignal("Footstep"):Connect(OnFootStep)
	animTracks["Run"]:GetMarkerReachedSignal("Footstep"):Connect(OnFootStep)

	animStoppedDetection()
	hitAWall()
	HRP.Touched:Connect(onTouched)
end

AnimationLoader()
1 Like

Set the animationtrack.Looped to false, in your system

3 Likes

I didn’t even know that, I thought the problem he had was normal thing.

3 Likes

I have already set it to false.

2 Likes

Could you show the script atleast?

2 Likes

I have edited my draft(broo, i hate this limit)

1 Like

It’s a normal thing.
But I think you could stop it by checking magnitude or smt I forget what it is.

if magnitude is too low or smt just don’t play anim

I don’t usually pro with magnitude thingy so yeah.

2 Likes

I dont really get what you mean, the magnitude of what exactly?

1 Like

could do something like this

1 Like

that really isnt my issue. I’m already using that in the script anyway and i already have a function that stops my character if i hit a wall. The problem is that the animation repeats itself while moving sideways while hitting the wall

1 Like

might just be your hitwall function thingy

1 Like

nah, this thing was the issue even before that function

1 Like

try detecting walking based on walkdirection

1 Like

you mean this: humanoid.MoveDirection.Magnitude?

1 Like

I personally prefer the non-stopping animation, the other one is glitchy and doesn’t look good

yes I am referring to that when it’s above 0 it means the human is walking

thats what im currently trying to fix

local function Movement()
	if humanoid.FloorMaterial ~= Enum.Material.Air or humanoid.FloorMaterial ~= Enum.Material.Water then
		if animTracks["Run"].isPlaying then
			animTracks["Walk"]:Stop()
		elseif animTracks["Walk"].isPlaying then
			animTracks["Run"]:Stop()

		end

		if humanoid.WalkSpeed <= 9 and humanoid.MoveDirection.Magnitude > 0 then
			animTracks["Run"]:Stop()
			animTracks["Walk"]:Play()

			if KeyDetection.pressed > 1 then
				local currentTime = tick()

				if lastPressedTime == nil then
					lastPressedTime = currentTime
				elseif currentTime - lastPressedTime < 0.5 then
					animTracks["Walk"].TimePosition = animationTrackStorage.Walk or 0
				elseif currentTime - lastPressedTime > 0.5 then	
					animTracks["Walk"].TimePosition = 0
				end

				lastPressedTime = currentTime
			end
		elseif humanoid.WalkSpeed > 9 and humanoid.MoveDirection.Magnitude > 0 then
			animTracks["Walk"]:Stop()
			animTracks["Run"]:Play()

			if KeyDetection.pressed > 1 then
				local currentTime = tick()

				if lastPressedTime == nil then
					lastPressedTime = currentTime
				elseif currentTime - lastPressedTime < 0.5 then
					animTracks["Run"].TimePosition = animationTrackStorage.Run or 0
				elseif currentTime - lastPressedTime > 0.5 then	
					animTracks["Run"].TimePosition = 0
				end

				lastPressedTime = currentTime
			end
		elseif humanoid.MoveDirection.Magnitude == 0 then
			animTracks["Walk"]:Stop()
			animTracks["Run"]:Stop()
			animTracks["Idle"]:Play()
		end
	end
end

i’m already checking for that when playing the anim

Oh really? By your title, it looks like you’re trying to stop repeating. I recommend you to change it to “Animation not repeating itself after running into a wall”

it is repeating but instead of being continous like the default roblox one, it just repeats itself from the start and it looks glitchy when you run into a wall