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