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

how do i use tick() to check how long a key was held? [closed]

Asked by 4 years ago

so basically i want to make a ability where the more u hold it the stronger and bigger it gets when it hits 4 sec or somehting it will fire the ability

thx for the help :D

0
This is not a request site. First, try scripting it, if it doesn't work, you can get help from here BestCreativeBoy 1395 — 4y

Closed as Not Constructive by BestCreativeBoy, imKirda, and DeceptiveCaster

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

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

Here is the code

Sorry for my english hope you understand me

local UserInput = game:GetService("UserInputService")
local Before = tick() --Current tick time
local Pressed = false --If key was pressed before

UserInput.InputBegan:Connect(function(Key,Processed)
    if not(Processed)then
        if(Key.KeyCode == Enum.KeyCode.B)then --Key, Enum.KeyCode[Key]
            Before = tick() --Current tick time
            Pressed = true --Key was pressed
            print("Key was pressed")
        end
    end
end)
UserInput.InputEnded:Connect(function(Key)
    if(Key.KeyCode == Enum.KeyCode.B)then --Key, the same as above
        if(Pressed)then --Check if Key was pressed before
            Pressed = false
            local HeldTime = tick() - Before --how long a key was held
            print("Key was helded for: " .. tostring(HeldTime) .. " seconds")
        end
    end
end)
Ad