For example, I have a tool, Fly, but I only want the player to access it if the double tap spacebar. How should I type the script?
✓ ANSWER ACCEPTED
Help with equiping tools using keydown events?
2 answers
✓ Accepted answer
An alternative to KeyDown is UserInputService. I had already helped you with something similar before so I won't go into too much detail as far as explaining goes. If you have any questions, comment on this reply and I will answer it in an edit.
local player = game:GetService("Players").LocalPlayer
local uis = game:GetService("UserInputService")
local key = "R" -- key to bind
local time = os.time()
local character = player.Character or player.CharacterAdded:wait()
uis.InputBegan:connect(function(inputObject, processed)
if not processed then
if inputObject.KeyCode == Enum.KeyCode[key] then
if(os.difftime(os.time(), time) < 0.8)then
warn("double pressed")
-- clone tool into
-- player's inventory/ backpack
end
time = os.time()
end
end
end)
Use the KeyDown event given by the player mouse object. You can get this object by calling GetMouse() on the local player (in local scripts only)
Here's an example:
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
mouse.KeyDown:connect(function(key) -- The "key" is what the user pressed
if key:lower()=="q" then
print(key.." was pressed")
end
end)
Now, if you're looking for double pressing space bar, you could use some debounce method as such:
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local between = 0.5 -- Time in which double tap is needed (in seconds)
local double = false
mouse.KeyDown:connect(function(key)
if key:byte()==32 then -- 32 is space bar
if double then
print("Double space")
else
double = false
wait(between)
double = true
end
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.
