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

how to make it so that the run animation plays only when BOTH CTRL AND W/A/S/D are pressed?

Asked by 2 years ago
Edited 2 years ago

this is my code for it

i cant seem to figure out how to incorporate the "isMoving" variable which determines if any of the directional keys are being pressed

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character
local hum = char.Humanoid
local UIS = game:GetService("UserInputService")
local camera = game.Workspace.CurrentCamera
local anim = script.sprint
local isMoving = false
local isSprinting = false

local animator = hum:FindFirstChildofClass("Animator")

local sprint = animator:LoadAnimation(anim)

UIS.InputBegan:Connect(function(key, isTyping)

    if isTyping then return end

    if key.KeyCode == Enum.KeyCode.LeftControl then
        if isSprinting == false then
            isSprinting = true
            char.Humanoid.WalkSpeed = 32

            for i=1,5 do
                camera.FieldOfView = 80+(i*3)
                wait()
            end

            sprint:Play()

        elseif isSprinting == true then
            isSprinting = false
            char.Humanoid.WalkSpeed = 16

            for i=1,5 do
                camera.FieldOfView = 95-(i*3)
                wait()
            end

            sprint:Stop()

        end
    end

end)

if UIS:IsKeyDown(Enum.KeyCode.W) then
    isMoving = true
elseif UIS:IsKeyDown(Enum.KeyCode.A) then
    isMoving = true
elseif UIS:IsKeyDown(Enum.KeyCode.S) then
    isMoving = true
elseif UIS:IsKeyDown(Enum.KeyCode.D) then
    isMoving = true
else
    isMoving = false
end

2 answers

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

Hello!

There are a few errors that you are facing with this code. For firsts, the IsKeyDown if statements will only run once, they should be shifted inside the InputBegan function, below the check for isTyping

Secondly, you are never checking if the isMoving variable is true or false when playing the sprint animation.

Addinng all of these simple fixes together they should look like this:

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character
local hum = char.Humanoid
local UIS = game:GetService("UserInputService")
local camera = game.Workspace.CurrentCamera
local anim = script.sprint
local isMoving = false
local isSprinting = false

local animator = hum:FindFirstChildofClass("Animator")

local sprint = animator:LoadAnimation(anim)

UIS.InputBegan:Connect(function(key, isTyping)

    if isTyping then return end

-- The following will now run everytime a key is pressed
if UIS:IsKeyDown(Enum.KeyCode.W) then
    isMoving = true
elseif UIS:IsKeyDown(Enum.KeyCode.A) then
    isMoving = true
elseif UIS:IsKeyDown(Enum.KeyCode.S) then
    isMoving = true
elseif UIS:IsKeyDown(Enum.KeyCode.D) then
    isMoving = true
else
    isMoving = false
end 

    if key.KeyCode == Enum.KeyCode.LeftControl then
        if isSprinting == false then
            isSprinting = true
            char.Humanoid.WalkSpeed = 32

            for i=1,5 do
                camera.FieldOfView = 80+(i*3)
                wait()
            end

    if (isMoving) then -- Is the value that we are setting by checking if the player is holding down W/A/S/D actually set to true?
            sprint:Play()
        end

        elseif isSprinting == true then
            isSprinting = false
            char.Humanoid.WalkSpeed = 16

            for i=1,5 do
                camera.FieldOfView = 95-(i*3)
                wait()
            end

            sprint:Stop()

        end
    end

end)

Hope this helps you!

Ad
Log in to vote
0
Answered by 2 years ago
local player,cam=game.Players.LocalPlayer,workspace.Camera
local char=player.Character or player.CharacterAdded:Wait()
local hum,mouse=char.Humanoid,player:GetMouse()
local ctrl,w,debounce,debounce2=false,false,false,false
local sprint=hum.Animator:LoadAnimation(script.sprint)
mouse.KeyDown:Connect(function(k)
    ctrl,w=k=="2"or ctrl,k=="w"or w
    if debounce then return end
    if ctrl and w then
        debounce=true
        hum.WalkSpeed+=16
        sprint:Play()
        for i=1,5 do
            cam.FieldOfView=80+(i*3)
            wait()
        end
    end
end)
mouse.KeyUp:Connect(function(k)
    ctrl,w=k~="2"and ctrl,k~="w"and w
    if not debounce then return end
    if(not ctrl or not w)then
        if debounce2 then return end
        debounce2=true
        hum.WalkSpeed-=16
        sprint:Stop()
    for i=1,5 do
        cam.FieldOfView = 95-(i*3)
        wait()
        end
        debounce,debounce2=false,false
        end
end)

Answer this question