Help with Animation

Hey People, i need help with a script.

I need if the Player is crouching and standing still, that he is just crouching without the Moving of the AnimationTrack but when the Player is moving while he is crouching the animationtrack should play as long as the player walks or until he stands up while walking.

In the Animation are: A active Loop and the priority “Movement”
and here is the LocalScript:

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

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
			animationTrack:Play()
			character.HumanoidRootPart.CanCollide = false
			crawlingValue.Value = true -- Set Crawling to true
		end
		setCrouchingEnabled(crouching)
		updateCamera()
	end
end)

RunService.RenderStepped:Connect(function()
	if crouching then
		updateCamera()
	end
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 help :slight_smile:

7 Likes

AnimationTracks have an Adjust Speed method which when set to 0 pauses the animation

Edit: In your case you need to do:

humanoid.Running:Connect(function(speed)
   if animationTrack.IsPlaying then animationTrack:AdjustSpeed(speed) end
end)
1 Like

How do i do it, i am not that good in animation scripting to be honest😅

1 Like

All you need to do is copy my script and add it somewhere at the bottom of your script for it to work :smile:

Thx but one last question, is there anyway to change the speed of the animation beacuse it looks like your running when the Track plays😂

Oops I forgot to add this:

humanoid.Running:Connect(function(speed)
   if animationTrack.IsPlaying then animationTrack:AdjustSpeed(speed / getCrawlSpeed()) end
end)

Replace the previous code with this, my mistake :sweat_smile:
Edit: this could also work so I suggest you try both:

humanoid.Running:Connect(function(speed)
   if animationTrack.IsPlaying then animationTrack:AdjustSpeed(speed / humanoid.WalkSpeed) end
end)
1 Like

Its really working but when i crouch out of the standing without moving its still playing the track until i move like an inch. Is there a way to fix this or should i just keep it like that?

Add this below the InputBegan connection:

UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode[keybind] and animationTrack.IsPlaying then animationTrack:Stop() end
end)

I dont know what i do wrong but now its all buggin

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
			animationTrack:Play()
			character.HumanoidRootPart.CanCollide = false
			crawlingValue.Value = true -- Set Crawling to true
			
			
		end
		UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
			if gameProcessedEvent then return end
			if input.KeyCode == Enum.KeyCode[keybind] and animationTrack.IsPlaying then animationTrack:Stop() end
		end)
		setCrouchingEnabled(crouching)
		updateCamera()
		
	end
end)

Like this lol:

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
			animationTrack:Play()
			character.HumanoidRootPart.CanCollide = false
			crawlingValue.Value = true -- Set Crawling to true
		end
		setCrouchingEnabled(crouching)
		updateCamera()
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode[keybind] and animationTrack.IsPlaying then animationTrack:Stop() end
end)

I did this before and there is the same bug, the Character is starting the animation but immidiatly stops it and stands up while the cam is still in the crouching mode

Have you tried maybe?.. Looping your animation? Try it in the script first above animationTrack:Play()

animationTrack.Looped = true
animationTrack:Play()

I just realised that the crouch is toggled so remove all the edits I made you do except the Humanoid.Running and replace the Humanoid.Running with either:

humanoid.Running:Connect(function(speed)
   if animationTrack.IsPlaying then
      if speed == 0 then animationTrack:Stop() else
         animationTrack:AdjustSpeed(speed / getCrawlSpeed())
      end
   end
end)

or

humanoid.Running:Connect(function(speed)
   if animationTrack.IsPlaying then
      if speed == 0 then animationTrack:Stop() else
         animationTrack:AdjustSpeed(speed / humanoid.WalkSpeed)
      end
   end
end)

depending on which one works best, sorry

No of them are working man, its okay i appreciate your help, i just take the first script you gave me and try to change it

Really thanks for the help :slight_smile:

1 Like

I think I found the solution finally! Use the first script I gave you like you said but add this at the very bottom:

crawlingValue.Changed:Connect(function(crawling)
   if not crawling then animationTrack:Stop() end
end)

Sorry it took me so long to find it :sweat_smile:

Its still not working but i have an idea, i will make that the Player moves an inch forward so it looks like it never startet the track if you understand

1 Like

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