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

How to make InputEnded event work only when the wanted key is lifted?

Asked by
Neon_N 104
5 years ago

The script below is localscript. Sprinting script works fine. but the problem is print("run") will print run multiple time when I only want it to print one time. and game.Workspace.CurrentCamera.FieldOfView = (80-(i*2)) Will work whenever a key(mouse click and other any keyboard key) is clicked. Please fix this.

local player = game.Players.LocalPlayer
local Char = player.Character or player.CharacterAdded:Wait()
local humanoid = Char:WaitForChild("Humanoid") -- Do not use script.Parent.Humanoid, use Char:WaitForChild("Humanoid")
local UserInputService = game:GetService("UserInputService")
local Running = false

UserInputService.InputBegan:Connect(function(Input, gameProcessed)
    if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and not gameProcessed then
        Running = true
        humanoid.WalkSpeed = 32
print("run")
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
            wait()
        end
    end
end)

UserInputService.InputEnded:Connect(function(Input, gameProcessed)
    if Input.KeyCode == Enum.KeyCode.LeftShift and not gameProcessed then
        Running = false
        humanoid.WalkSpeed = 16
        if Running == false then
            for i = 1,5 do
                game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
                wait()
            end
        end
    end
end)

1 answer

Log in to vote
0
Answered by
Sorukan 240 Moderation Voter
5 years ago

For your InputBegan script change it to this

    UserInputService.InputBegan:Connect(function(Input, gameProcessed)
        if input.KeyCode = Enum.KeyCode.LeftShift and not gameProcessed then
            Running = true
            humanoid.WalkSpeed = 32
    print("run")
            for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
                wait()
        end
        end
    end)

you basically do the same as your inputended

0
For 2 seconds I thought you put "pun intended", but it just says "inputended" LonelyScripter 4 — 5y
Ad

Answer this question