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()