Animation Problem

Hello Guys, i have an issue with my Crouching animation. The Animation is Looped and has the Priority “Movement” and i want that when the Player is crouching and moving (Like Walking) that the Animationtrack is Playing and when the Player is not walking and just crouching i want that the Animation is playing but not the Animationtrack (If you understand)

Can someone Help me fix this local script because i have no idea anymore how to recode this:


--// Configuration
local keybind = "C"
local animationId = 15543420246 --> Replace the number with your animation ID!

-- Define the crawl speed for players with different roles
local rogueCrawlSpeed = 4
local otherCrawlSpeed = 8

--// Variables
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local crawlingValue = player:WaitForChild("Crawling")

local animation = Instance.new("Animation")
animation.AnimationId = 'rbxassetid://' .. animationId

local animationTrack = humanoid:WaitForChild("Animator"):LoadAnimation(animation)
animationTrack.Priority = Enum.AnimationPriority.Action
animationTrack.Looped = true -- Ensure the animation loop is enabled

local crouching = false
local originalJumpPower = humanoid.JumpPower
local cameraOffset = Vector3.new(0, 1.5, 0) -- Adjust this to set the camera height offset

local function updateCamera()
	local camera = workspace.CurrentCamera
	if crouching then
		camera.CameraSubject = character.HumanoidRootPart
		camera.CameraType = Enum.CameraType.Custom
		camera.CFrame = camera.CFrame - cameraOffset
	else
		camera.CameraSubject = character.Humanoid
		camera.CameraType = Enum.CameraType.Follow
	end
end

local function setCrouchingEnabled(enabled)
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, not enabled)
end

local function getCrawlSpeed()
	-- Check if the player's "Role" StringValue exists and set crawl speed accordingly
	local roleValue = player:FindFirstChild("Role")
	if roleValue then
		if roleValue.Value == "Rogue" then
			return rogueCrawlSpeed
		else
			return otherCrawlSpeed
		end
	end
	return otherCrawlSpeed
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode[keybind] then
		if crouching then
			humanoid.WalkSpeed = 16
			humanoid.JumpPower = originalJumpPower
			crouching = false
			animationTrack:Stop()
			character.HumanoidRootPart.CanCollide = true
			crawlingValue.Value = false -- Set Crawling to false
		else
			-- Set crawl speed based on player's role
			humanoid.WalkSpeed = getCrawlSpeed()
			humanoid.JumpPower = 0
			crouching = true
			character.HumanoidRootPart.CanCollide = false
			crawlingValue.Value = true -- Set Crawling to true
		end
		setCrouchingEnabled(crouching)
		updateCamera()
	end
end)

RunService.RenderStepped:Connect(function()
	-- Check if the player is moving (WalkSpeed > 0) to play or stop the animation
	if crouching and humanoid.WalkSpeed > 0 then
		-- Play the animation loop only when crouching and walk speed is greater than 0
		animationTrack:Play()
	else
		animationTrack:Stop()
	end

	updateCamera()
end)

-- Reset camera when not crouching
humanoid.StateChanged:Connect(function(oldState, newState)
	if newState ~= Enum.HumanoidStateType.Swimming and newState ~= Enum.HumanoidStateType.Climbing and not crouching then
		updateCamera()
	end
end)


Thx for everyone trying to help me :slight_smile:

1 Like

How about using Humanoid.MoveDirection to detect when a Player is moving?

local player = game:GetService(“Players”).LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

-- Add other variables and scripts

local function detectMovementwhileCrouching()

    if humanoid.MoveDirection.Magnitude > 0 then -- Player is moving
         animation:Play()
         animationTrack:Stop()
    else -- Player isn’t moving
         animationTrack:Play()
         animation:Stop()
    end
end
1 Like

When i add you script part the animation is not even playing anymore

--// Configuration
local keybind = "C"
local animationId = 15543420246 --> Replace the number with your animation ID!

-- Define the crawl speed for players with different roles
local rogueCrawlSpeed = 4
local otherCrawlSpeed = 8

--// Variables
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local crawlingValue = player:WaitForChild("Crawling")

local animation = Instance.new("Animation")
animation.AnimationId = 'rbxassetid://' .. animationId

local animationTrack = humanoid:WaitForChild("Animator"):LoadAnimation(animation)
animationTrack.Priority = Enum.AnimationPriority.Action
animationTrack.Looped = true -- Ensure the animation loop is enabled

local crouching = false
local originalJumpPower = humanoid.JumpPower
local cameraOffset = Vector3.new(0, 1.5, 0) -- Adjust this to set the camera height offset

local function updateCamera()
	local camera = workspace.CurrentCamera
	if crouching then
		camera.CameraSubject = character.HumanoidRootPart
		camera.CameraType = Enum.CameraType.Custom
		camera.CFrame = camera.CFrame - cameraOffset
	else
		camera.CameraSubject = character.Humanoid
		camera.CameraType = Enum.CameraType.Follow
	end
end

local function setCrouchingEnabled(enabled)
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, not enabled)
end

local function getCrawlSpeed()
	-- Check if the player's "Role" StringValue exists and set crawl speed accordingly
	local roleValue = player:FindFirstChild("Role")
	if roleValue then
		if roleValue.Value == "Rogue" then
			return rogueCrawlSpeed
		else
			return otherCrawlSpeed
		end
	end
	return otherCrawlSpeed
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode[keybind] then
		if crouching then
			humanoid.WalkSpeed = 16
			humanoid.JumpPower = originalJumpPower
			crouching = false
			animationTrack:Stop()
			character.HumanoidRootPart.CanCollide = true
			crawlingValue.Value = false -- Set Crawling to false
		else
			-- Set crawl speed based on player's role
			humanoid.WalkSpeed = getCrawlSpeed()
			humanoid.JumpPower = 0
			crouching = true
			character.HumanoidRootPart.CanCollide = false
			crawlingValue.Value = true -- Set Crawling to true
		end
		setCrouchingEnabled(crouching)
		updateCamera()
	end
end)

local function detectMovementwhileCrouching()

	if humanoid.MoveDirection.Magnitude > 0 then -- Player is moving
		animation:Play()
		animationTrack:Stop()
	else -- Player isn’t moving
		animationTrack:Play()
		animation:Stop()
	end
end

	updateCamera()


-- Reset camera when not crouching
humanoid.StateChanged:Connect(function(oldState, newState)
	if newState ~= Enum.HumanoidStateType.Swimming and newState ~= Enum.HumanoidStateType.Climbing and not crouching then
		updateCamera()
	end
end)
1 Like

Apologies, I’ve updated parts of the code:


UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode[keybind] then
		if crouching then
			humanoid.WalkSpeed = 16
			humanoid.JumpPower = originalJumpPower
			crouching = false
			animationTrack:Stop()
			character.HumanoidRootPart.CanCollide = true
			crawlingValue.Value = false -- Set Crawling to false
		else
			-- Set crawl speed based on player's role
			humanoid.WalkSpeed = getCrawlSpeed()
			humanoid.JumpPower = 0
			crouching = true
			character.HumanoidRootPart.CanCollide = false
			crawlingValue.Value = true -- Set Crawling to true
		end
		setCrouchingEnabled(crouching)
		updateCamera()
        detectMovementwhileCrouching()
	end
end)


local function detectMovementwhileCrouching()
  repeat
	if humanoid.MoveDirection.Magnitude > 0 then -- Player is moving
		animation:Play()
		animationTrack:Stop()
	else -- Player isn’t moving
		animationTrack:Play()
		animation:Stop()
	end
  until crouching = false
1 Like