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"
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)
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.
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.