Default roblox walking animation overwrites custom one

  1. What do you want to achieve? Keep it simple and clear!

I want the crouch-walking animation to play when the player is both crouching and walking (:exploding_head:)

  1. What is the issue? Include screenshots / videos if possible!

While crouch-walking, the default roblox walking animation plays instead of the custom one.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried tweaking the animation’s priorities, but that did not seem to help.

Code:

local plr = game.Players.LocalPlayer
local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local crouch_idle_anim = animator:LoadAnimation(script:WaitForChild("IdleCrouch"))
local TS = game:GetService("TweenService")
local crouch_walk_anim = animator:LoadAnimation(script:WaitForChild("CrouchAnimation"))
local UIS = game:GetService("UserInputService")
local IsCrouchWalking = false

local crouching_offset = Vector3.new(0,-1,-1)
local normal_offset = Vector3.new(0,0,-1)


crouch_idle_anim.Looped = true
crouch_walk_anim.Looped = true

UIS.InputBegan:Connect(function(input,gpe)
	if not gpe then
		if input.KeyCode == Enum.KeyCode.C then
			if plr:GetAttribute("crouching") == false then
				plr:SetAttribute("crouching",true)
				print("crouching")
			--	for i,v in pairs(char:GetChildren()) do
			--		if v:IsA("BasePart") then
			--			TS:Create(v,TweenInfo.new(0.5),{LocalTransparencyModifier = 1}):Play() -- Hide the player's body parts so they don't cover his camera
			--		end
			--	end
				TS:Create(humanoid,TweenInfo.new(0.5),{CameraOffset = crouching_offset}):Play() -- lower the camera a little bit
				crouch_idle_anim:Play()
				
			else
				crouch_idle_anim:Stop()
				print("uncrouching")
				local uncrouchTween = TS:Create(humanoid,TweenInfo.new(0.5),{CameraOffset = normal_offset}) -- recalibrate the camera when uncrouching
				uncrouchTween:Play()
			--		for i,v in pairs(char:GetChildren()) do
			--			if v:IsA("BasePart") and v.Name ~= "Head" then
			--				TS:Create(v,TweenInfo.new(0.5),{LocalTransparencyModifier = 0}):Play() -- make the player's limbs reappear (apart from the head, so it doesn't cover the camera)
			--			end
			--		end
				uncrouchTween.Completed:Connect(function()
				plr:SetAttribute("crouching",false)
				end)
			end
		end
	end
end)

plr:GetAttributeChangedSignal("crouching"):Connect(function()
	if plr:GetAttribute("crouching") == true then
		while task.wait() do
			if humanoid.MoveDirection.magnitude > 0 then
				if not IsCrouchWalking then
					crouch_walk_anim:Play()
					IsCrouchWalking = true
					print("crouch walking")
				end
			else crouch_walk_anim:Stop()
				
			end
		end
	end 
end)

Thanks for your time :slight_smile:

2 Likes

You need to increase the priority of your crouch walking animation. Roblox core scripts always use core level animation priority, so if you set the priority to anything higher then it will play over the Roblox one.

Edit: AnimationPriority

EditEdit: Oh, you already tried that? I will review your code real quick but double check that your animation is using a priority higher than core.

EditEditEdit: it appears that the idle is working but the walking one is not. I don’t see any evidence that the walking one is playing at all (unless the priority is too low). Does “crouch walking” ever print? I would add a print to the second function as well where it ends the walking animation and make sure it’s not playing that right after the walking anim starts.

2 Likes

I added another print statement to the 2nd function and realised that the animation was stopping itself as soon as it started (Not going to talk about the lag it was creating as well).

I changed the 2nd function entirely, and I ended up with this, which seems to work smoothly.

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
		if plr:GetAttribute("crouching") == true then
		if humanoid.MoveDirection.magnitude == 0 then
			if IsCrouchWalking then
				IsCrouchWalking = false
				print("stopped crouch walking")
				wait(.05)
				crouch_walk_anim:Stop()
				crouch_idle_anim:Play()
			end
				else
					if not IsCrouchWalking then
						IsCrouchWalking = true
						print("crouch walking")
					wait()
						crouch_idle_anim:Stop()
						crouch_walk_anim:Play()
					end
			end
		end
end)

Thanks alot for your help :slight_smile:

1 Like

No problem! Glad you were able to solve it with that information. Sometimes when we are debugging we forget to use prints thoroughly enough to reveal the issue. Let this be a lesson to do a little more diligence and you will save many hours of debugging and lots of headaches! :joy:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.