Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Nothing will print in "Mouse.KeyDown:Connect" script?

Asked by 6 months ago

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)
0
Use local variables, key down is deprecated use userinputservice DinozCreates 746 — 6mo

2 answers

Log in to vote
0
Answered by
yHasteeD 1573 Moderation Voter
6 months ago

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 :)

Ad
Log in to vote
2
Answered by 6 months ago

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)

Answer this question