So, I’ve got a script where I’m trying to detect if a user holds down control+L, but cant seem to get it right.
What I’ve tried:
Having a “ISHOLDING” varible, issue is it doesnt detec the “L” if control is down
and
if uis:IsKeyDown(Enum.KeyCode.LeftControl) and uis:IsKeyDown(Enum.KeyCode.L) then
All help appreciated.
Try this may need to be edited to work for your script:
-- Assuming this code is inside a LocalScript (which handles user input)
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input, isProcessed)
if not isProcessed then
if input.KeyCode == Enum.KeyCode.LeftControl then
local isCtrlPressed = true
uis.InputEnded:Connect(function(innerInput)
if innerInput.KeyCode == Enum.KeyCode.LeftControl then
isCtrlPressed = false
end
end)
uis.InputBegan:Connect(function(innerInput2)
if isCtrlPressed and innerInput2.KeyCode == Enum.KeyCode.L then
-- Ctrl+L is pressed
print("Ctrl + L pressed")
end
end)
end
end
end)
You wouldnt know how to use Robloxs enum modifier keys? It was mentioned in other posts I looked at
ModifierKey | Documentation - Roblox Creator Hub
This didn’t work. The print functions I put in aren’t going. Ill leave the full script
local isHolding = false
local veh = game.Workspace:FindFirstChild(script.Parent.Parent.CarName.Value, true).Body
local mouse=game.Players.LocalPlayer:GetMouse()
local current = "Staff"
local uis = game:GetService("UserInputService")
--// J key, Stages
-- Assuming this code is inside a LocalScript (which handles user input)
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input, isProcessed)
if not isProcessed then
if input.KeyCode == Enum.KeyCode.LeftControl then
local isCtrlPressed = true
uis.InputEnded:Connect(function(innerInput)
if innerInput.KeyCode == Enum.KeyCode.LeftControl then
isCtrlPressed = false
end
end)
uis.InputBegan:Connect(function(innerInput2)
if isCtrlPressed and innerInput2.KeyCode == Enum.KeyCode.L then
print("made it to check if L")
print("made it past is holding")
if current == "Staff" then
current = "Police"
veh.Lightbar.Remotes.ELSSwitchEvent:FireServer(current)
print("fired")
elseif current == "Police" then
current = "Staff"
veh.Lightbar.Remotes.ELSSwitchEvent:FireServer(current)
end
end
end)
end
end
end)
UserInputService.InputBegan:Connect(function(input, processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.L and UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) then
-- Left Control + L
end
end)
If you want L
to be inputted second, detect if the new input is L, then check if Left Control is held down (which is what the code sample I gave does)
Creating nested event connections like that will create memory leaks
Like what memory would even be leaked? Also why would it be leaked?
You’re creating two new event connections every time UserInputService.InputBegan
fires; this will multiply and continue firing the old events, even if they aren’t used