Hey, I'm testing a script, but when I press Z nothing is printed. I'm trying to add a delay when I press Z, but nothing will print... Please help
local repstore = game:WaitForChild("ReplicatedStorage") local Remote = repstore:WaitForChild("IceStart") Player = game.Players.LocalPlayer Mouse = Player:GetMouse() cool = true Mouse.KeyDown:Connect(function(key) key = key:lower() if key == "z" then if cool == true then return end cool = false print("Working") wait(5) cool = true end end)
Try to use UserInputService
the Mouse.KeyDown
is deprecated
You can see more here: UserInputService - Wiki
Remember, UserInputService can used in LocalScript or ModuleScript. not work in Server Script
here is a example:
local service = game:GetService("UserInputService") service.InputBegan:Connect(function(input,gameproc) if not gameproc then -- (gameProcessed) if input.KeyCode == Enum.KeyCode.F then print("You pressed F") end end end) service.InputBegan:Connect(function(input,gameproc) if not gameproc then -- (gameProcessed) if input.KeyCode == Enum.KeyCode.F then wait(0.07) print("You stoped with pressing F") end end end)
You can try this:
local service = game:GetService("UserInputService") local debounce = true service.InputBegan:Connect(function(input,gameproc) if not gameproc and debounce == true then -- (gameproc = gameProcessed) if input.KeyCode == Enum.KeyCode.Z then debounce = false print("Working") wait(5) debounce = true end end end)
Hope it helped :)
Use UserInputService
Like that
local UserInputService = game:GetService('UserInputService') local debounce = true UserInputService.InputBegan:Connect(function(input) if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.Z and debounce then debounce = false print("Working") wait(5) debounce = true end end)