Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

1 comment

0

This is not a request site. First, try scripting it, if it doesn't work, you can get help from here

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.