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

Why won't my "key Press" function not work?

Asked by 4 years ago

Hello, I think this has an easy fix but

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key:lower() == "l" or key:upper() == "L" then
        game.SoundService.TheBox.Play = true --The sound
    end
end)

I am trying to access a sound in SoundService by pressing the key "L" or "l" but it doesn't work...

More Information

-It is a local Script

-The Local script is inside a text label

-"The Box" Music is found in "SoundService"

0
I recommend posting the error as well. If you have one in the output. It helps a decent amount in solving the issue. SethHeinzman 284 — 4y
0
you ars trying to know if the player pressed L in keyboard? why need the nouse then? cause if so, you can use UserInputService Necro_las 412 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago

Its simple,

local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.A then -- Replace 'A' with your key
--run what you want to happen
end
end)

(ina LocalScript)

Ad
Log in to vote
0
Answered by
DollorLua 235 Moderation Voter
4 years ago

mouse.KeyDown I have never used, I don't think its a function on roblox either.

I recommend using UIS, or User Input Service. It is very simple to use.

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(processed, input)
    if not processed then
    if input.KeyCode == Enum.KeyCode.L then
        -- run code here.
    end
end)

KeyCode is always the lowercase letter. Pressing shift + l will still run your code.

the not processed part tells the script that if the player is typing into a text box (chat for example) it won't run the code since they didn't want to execute the code.

0
mouse.KeyDown is a function, but UserInputService is recommended over using mouse. killerbrenden 1537 — 4y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

KeyDown is the old way I am pretty sure.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Sound = game.SoundService.TheBox



mouse.KeyDown:connect(function(key)
    if key:lower() == "l" or key:upper() == "L" then
        Sound:Play()
    end
end)

Here is the new way using userinputservice

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Sound = game.SoundService.TheBox
local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, processed)
    if input.KeyCode == Enum.KeyCode.L then
        Sound:Play()
    end
end)

It also might be input = Enum.KeyCode.L . I forget but if it doesn’t work again try that.

0
this is just my code but with player, mouse, and sound put on top. Please don't steal code. This is an assumption but it is exactly the same as mine. Sorry if I'm incorrect. DollorLua 235 — 4y
0
It’s not exactly the same. I did use yours as a template so I’d didn’t have to write it out. I improved on yours by including what he needed. lol SethHeinzman 284 — 4y
0
neither of you codes work lol Smartics 25 — 4y
0
Try switching input and processed. He typed it wrong. SethHeinzman 284 — 4y
View all comments (4 more)
0
Just changed it so if it doesn’t work uhh I’m bad. SethHeinzman 284 — 4y
0
I wish someone just explained and not slapped a code at me and expect i understand Smartics 25 — 4y
0
We did. You showed you are capable of looking at corrected code and understanding. What is the error that you are receiving? SethHeinzman 284 — 4y
0
I’m just in a car right now so hard to type a lot lol. SethHeinzman 284 — 4y

Answer this question