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

How to make a KeyDown function, but you have to hold down the button for a certain time ?

Asked by 6 years ago
Edited 6 years ago

I'm no genius at this part... I'd like a script you put inside bricks/studs where you must hold down a keyboard button within it's limited reach (You have to be ~10 studs away, hold down E - to make something happen). Is it something like this:

local Loot = script.Parent
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Distance = ((Player.Position-Loot.Position).Magnitude)
local Icon = Loot.BillboardGui

Mouse.KeyDown:connect(function(key)
    if key == "e" and Distance <=10 then

end
end)
0
So like Jailbreak? User#21242 20 — 6y
0
Scripting helpers is not a request site Rare_tendo 3000 — 6y
0
Basically like Jailbreak, yes ! LordTechet 53 — 6y

2 answers

Log in to vote
0
Answered by
Vexture 179
6 years ago
Edited 6 years ago

Hey there! I'll walk you through this with sample code and explanations. I'll assume you know how for loops and RenderStepped works as well as activating functions through events. I'll walk you through UserInputService, as KeyDown is deprecated!

First thing you're going to want to do:

Create a variable that is set to true or false depending on whether or not "E" is being pressed (I suggest calling it "epressed" or something like that)

Oh, and I also suggest using UserInputService, as KeyDown is deprecated. Here's an example of how you'd set that up:

game:GetService("UserInputService").InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        epressed = true
    end
end)

game:GetService("UserInputService").InputEnded:connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        epressed = false
    end
end)

Basically, InputBegan marks a key press, while InputEnded marks the end of a keypress. Now, when epressed == true, you want another variable that's basically a counter for how long you've held down the key. Name it ecounter or something like that. Set ecounter = 0 by default.

In a separate function that you should write above the UserInputService functions, write a for loop that counts from 1 to however long you want to wait. Sample code:

epressed = false
ecounter = 0

function countE()
    for i = 1,3,1 do
        if epressed == true then
            ecounter = i
            print(ecounter)
        end
        if epressed == false then
            break
        end
        if ecounter == 3 then
            --Do whatever you wanted to accomplish here! E has been pressed for the amount of time you want!
        end
            wait(1)
    end
end

game:GetService("UserInputService").InputBegan:connect(function(input) --E is pressed
    if input.KeyCode == Enum.KeyCode.E then
        epressed = true
        spawn(countE) --Function countE is run in a new thread. You don't have to do this, but I did in case restarting the function each time messed with anything.
    end
end)

game:GetService("UserInputService").InputEnded:connect(function(input) --E is releaseed
    if input.KeyCode == Enum.KeyCode.E then
        epressed = false
    end
end)

Pretty simple! It only counts up if E is being pressed down, and if not, it breaks out of the for loop! We also check if ecounter has actually reached the amount of time we want (in this example, 3 seconds) and within that condition you can do whatever you wanted the script to do once you pressed E for the desired amount of t ime. Now, in a RenderStepped event function using RunService, I made it constantly set ecounter = 0 while E is not being pressed. Using RenderStepped makes it accurate up to each frame that's rendered. Sample:

game:GetService("RunService").RenderStepped:connect(function()
    if epressed == false then
        ecounter = 0
    end
end)

Here's the whole sample script in action!

epressed = false
ecounter = 0

function countE()
    for i = 1,3,1 do
        if epressed == true then
            ecounter = i
            print(ecounter)
        end
        if epressed == false then
            break
        end
        if ecounter == 3 then
            --Do whatever you wanted to accomplish here! E has been pressed for the amount of time you want!
        end
            wait(1)
    end
end

game:GetService("UserInputService").InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        epressed = true
        spawn(countE)
    end
end)

game:GetService("UserInputService").InputEnded:connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        epressed = false
    end
end)

game:GetService("RunService").RenderStepped:connect(function()
    if epressed == false then
        ecounter = 0
    end
end)

This is probably not the most elegant solution in the world, but it definitely works. I spent ~20 minutes writing this to make sure I didn't leave anything out. I wrote this hoping that you and people looking up this question in the future could follow along easily! If you have any more questions, please let me know. If anybody thinks I should revise this answer, tell me! Thanks! :)

Ad
Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Mouse.KeyDown and Mouse.KeyUp are deprecated. I advise you use UserInputService or ContextActionService.

local brick = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character.HumanoidRootPart
local cas = game:GetService('ContextActionService')
local RS = game:GetService('RunService')
local TIME = 5
local t = nil

cas:BindAction('EPress',function(a,s,k)
        if s == Enum.UserInputState.Begin then
               if (hrp.Position - brick.Position).magnitude <= 10 then
                      t = tick()
               end
        elseif s == Enum.UserInputState.End then
               t = nil
        end
end,false,Enum.KeyCode.E)

RS.RenderStepped:Connect(function()
        if (hrp.Position - brick.Position).magnitude > 10 then t = nil end
        if t then
               if (tick()-t) >= TIME then
                      print('do stuff')
                      t = nil
               end 
        end
end)
0
Nothing... LordTechet 53 — 6y

Answer this question