Question: Is there a way to determine the amount of time a key was held for?
Understanding: No, I do not want to print out a value each time the key was pressed, but I do wish to have a private timer that is able to count in seconds the amount of time a user utilized it. Then, I want that time to subtract the value of value1.
This is the code that I am using -
These lines of code change the amount of value1 when S is utilized.
I reccomend using UserInputService.InputBegan and UserInputService.InputEnded. When InputBegan is fired, save a variable that contains the current tick(). Then when InputEnded is fried, save the newest tick() to another variable. Subtract the last tick from the first tick and you get the time that the player has held down the button.
You want to declare the variable outside of the InputBegan scope. Define the variable inside of InputBegan.
Then you’d want to do the subtraction bit inside of InputEnded as @AxoLib pointed out.
local UserInputService = game:GetService("UserInputService")
local STime
UserInputService.InputBegan:Connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.S then
STime = tick()
end
end)
UserInputService.InputEnded:Connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.S then
local TTime = tick() - STime
end
end)