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
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)
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
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! :)
3 comments
So like Jailbreak?
Scripting helpers is not a request site
Basically like Jailbreak, yes !