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

How would I find out when the player has stopped pressing any keys?

Asked by
Zrxiis 26
4 years ago

I'm trying to make my script change the animations for the player when they move, but can't figure out how I would check if the player stopped pressing keys and make the idle animation play.

local player = game.Players.LocalPlayer
local character = player.Character
local UserInputService = game:GetService("UserInputService")

if not character or character.Parent then
    repeat
        wait()
    until character
end


local humanoid = character:WaitForChild("Humanoid")
humanoid.WalkSpeed = 12
local animationTrack = Instance.new("Animation")
local running = false
local currentAction = nil

local animations = {["walk"] = "rbxassetid://4867116428",
    ["run"] = "rbxassetid://4879512283",
    ["idle"] = "rbxassetid://4867149545",
    ["jump"] = "rbxassetid://04879987572",
    ["climb"] = "rbxassetid://180436334"}

animationTrack.AnimationId = animations["idle"]
local animation = humanoid:LoadAnimation(animationTrack)
animation:Play()

local movementKeys = {Enum.KeyCode.W,Enum.KeyCode.A,Enum.KeyCode.S,Enum.KeyCode.D}

local function playAnimation(input, gameProccessed)
    if gameProccessed == false then
        if input.UserInputType == Enum.UserInputType.Keyboard then
            for i=1,#movementKeys do
                if input.KeyCode == movementKeys[i] then
                    if running == false then
                        animationTrack.AnimationId = animations["walk"]
                        local animation = humanoid:LoadAnimation(animationTrack)
                        animation:Play()
                    else
                        animationTrack.AnimationId = animations["run"]
                        local animation = humanoid:LoadAnimation(animationTrack)
                        animation:Play()
                    end         
                end
            end 
            if input.KeyCode == Enum.KeyCode.Space then
                animationTrack.AnimationId = animations["jump"]
                local animation = humanoid:LoadAnimation(animationTrack)
                currentAction = "jump"
                animation:Play()
            end
            if input.KeyCode == Enum.KeyCode.LeftShift then
                animationTrack.AnimationId = animations["run"]
                local animation = humanoid:LoadAnimation(animationTrack)
                animation.Name = "run"
                animation:Play(0.5)
                humanoid.WalkSpeed = 30
                running = true
            end
        end
    end
end


local function stopAnimation(input, gameProccessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.LeftShift then
            for i,v in pairs(humanoid:GetPlayingAnimationTracks()) do
                if v.Name == "run" then
                    v:Stop(0.5)
                end
            end
            humanoid.WalkSpeed = 12
            running = false
        end
    end
end


UserInputService.InputBegan:Connect(playAnimation)
UserInputService.InputEnded:Connect(stopAnimation)

0
You can simply attach a Listener to the Idle HumanoidStateType. Ziffixture 6913 — 4y

1 answer

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

If you want to find out when the player stopped pressing any keys (including movement key, number key or even anything) you need to do the opposite of pressing any keys by using not code. Here is the code. (Insert this code into script not replace entire script)

if not input.UserInputType == Enum.UserInputType.Keyboard then
    animationTrack.AnimationId = animations["idle"]
    local animation = humanoid:LoadAnimation(animationTrack)
    animation:Play()
end

EDIT: If you don't know where to put then I will mark the line to put with comment

local player = game.Players.LocalPlayer
local character = player.Character
local UserInputService = game:GetService("UserInputService")

if not character or character.Parent then
    repeat
        wait()
    until character
end


local humanoid = character:WaitForChild("Humanoid")
humanoid.WalkSpeed = 12
local animationTrack = Instance.new("Animation")
local running = false
local currentAction = nil

local animations = {["walk"] = "rbxassetid://4867116428",
    ["run"] = "rbxassetid://4879512283",
    ["idle"] = "rbxassetid://4867149545",
    ["jump"] = "rbxassetid://04879987572",
    ["climb"] = "rbxassetid://180436334"}

animationTrack.AnimationId = animations["idle"]
local animation = humanoid:LoadAnimation(animationTrack)
animation:Play()

local movementKeys = {Enum.KeyCode.W,Enum.KeyCode.A,Enum.KeyCode.S,Enum.KeyCode.D}

local function playAnimation(input, gameProccessed)
    if gameProccessed == false then
        if input.UserInputType == Enum.UserInputType.Keyboard then
            for i=1,#movementKeys do
                if input.KeyCode == movementKeys[i] then
                    if running == false then
                        animationTrack.AnimationId = animations["walk"]
                        local animation = humanoid:LoadAnimation(animationTrack)
                        animation:Play()
                    else
                        animationTrack.AnimationId = animations["run"]
                        local animation = humanoid:LoadAnimation(animationTrack)
                        animation:Play()
                    end         
                end
            end 
            if input.KeyCode == Enum.KeyCode.Space then
                animationTrack.AnimationId = animations["jump"]
                local animation = humanoid:LoadAnimation(animationTrack)
                currentAction = "jump"
                animation:Play()
            end
            if input.KeyCode == Enum.KeyCode.LeftShift then
                animationTrack.AnimationId = animations["run"]
                local animation = humanoid:LoadAnimation(animationTrack)
                animation.Name = "run"
                animation:Play(0.5)
                humanoid.WalkSpeed = 30
                running = true
            end
            if not input.UserInputType == Enum.UserInputType.Keyboard then -- Here
                animationTrack.AnimationId = animations["idle"]
                local animation = humanoid:LoadAnimation(animationTrack)
                animation:Play()
            end
        end
    end
end


local function stopAnimation(input, gameProccessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.LeftShift then
            for i,v in pairs(humanoid:GetPlayingAnimationTracks()) do
                if v.Name == "run" then
                    v:Stop(0.5)
                end
            end
            humanoid.WalkSpeed = 12
            running = false
        end
    end
end


UserInputService.InputBegan:Connect(playAnimation)
UserInputService.InputEnded:Connect(stopAnimation)

If this answer helped you then please accept the answer!

0
Where should I put this because if I put it in the InputEnded function it doesnt do anything. Zrxiis 26 — 4y
0
At line 60 after line 59 (put at end) because if you put early it will cause to pressing any key doesn't do any animation Somone_exe 224 — 4y
0
i will update the answer Somone_exe 224 — 4y
Ad

Answer this question