Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make it so that the moving-while-crouching-animation plays only if you move?

Asked by 3 years ago

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?

0
the ends would help a little Leamir 3138 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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

0
another thing, basically i scripted it so that you could aim in with a tool with a model gun that i modeled myself through blender, i also made a tween animation so that the field of view is lowered, but how do you lower the sensitivity? i tried mousedeltasensitvity, but how do you return it to its previous properties when the user stops rightclicking or aiming in? xxIamInevitable 2 — 3y
0
also, when i don't move before i crouch, and i try to move, the idle crouch still plays. same thing goes for if i was walking and crouched xxIamInevitable 2 — 3y
0
that is in your own script, when you play the animation, like run, you don't stop the idle and in the idle, you dont stop the run anim AnasBahauddin1978 715 — 3y
0
does not work xxIamInevitable 2 — 3y
Ad

Answer this question