Basically, I've made it so that if you hold LCtrl (using UIS) it lets you crouch, but the problem is, the moving-while-crouching-animation plays even when you're not moving. I've made use of GetState() but it doesn't seem to work. Here's the block of code.
------------------------- ----VARIABLES---- ------------------------- local UIS = game:GetService("UserInputService") local player = game.Player.LocalPlayer local character = player.Character local humanoid = character:WaitForChild("Humanoid") crouched = false moving = false -------------------------- ----ANIMATIONS---- -------------------------- local crouchedanim = Instance.new("Animation") crouchedanim.Parent = character crouchedanim.Name = "CrouchedAnim" crouchedanim.AnimationId = "rbxassetid://556017" local crouchedtrack = humanoid:LoadAnimation(crouchedanim) local crouchmoving = Instance.new("Animation") crouchmoving.Parent = character crouchmoving.Name = "MovingCrouchAnim" crouchmoving.AnimationId = "rbxassetid://551730" local crouchmovetrack = humanoid:LoadAnimation(crouchedanim) UIS.InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.LeftControl then crouched = true if not moving then crouchedtrack:Play() humanoid.WalkSpeed = 5 elseif moving then crouchmovetrack:Play() UIS.InputEnded:Connect(function(key) if key.KeyCode == Enum.KeyCode.LeftControl then crouched = false crouchedtrack:Stop() crouchmovetrack:Stop() humanoid.WalkSpeed = 16 humanoid.StateChanged:Connect(function(oldState, newState) if newState == Enum.HumanoidStateType.Running then moving = true else moving = false
ignore the fact that there are no ends or spacing, i just typed it up here cause i have no time to open roblox studio (and i am 100% sure that that's the exact code)
Basically, the problem here is that the moving-while-crouched animation still plays even when the player is not running. How do I fix this, how do I make it work?
theres an event called humanoid.Running which runs when the player is running, you can use the first parameter which is speed, if the speed is 0, that means player is standing still, so try replacing the stateChanged event with running event
humanoid.Running:Connect(function(speed) if speed > 0 then -- Running moving = true else -- Stopped moving = false end end)
you are also missing a lot of ends
Edit:
you didn't stop the other anim
UIS.InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.LeftControl then crouched = true if moving then crouchedtrack:Stop() crouchmoving:Play() else crouchedtrack:Play() crouchmoving:Stop() end end end