I’ve used UserInputService before lots of times and I don’t understand why it’s firing twice on me. I’ve looked on other forums, about this exact problem, but I’ve already included debounces and made sure that the script was written right. I’ve finally decided to ask everyone else about this. To quickly recap what my problem is, whenever I press any key on the keyboard, the UserInputService will call it twice.
local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer
local remoteEventsFolder = replicatedStorage.RE
local angelInputEvent = remoteEventsFolder:WaitForChild("AngelInputEvent")
local mouse = player:GetMouse()
local firstPowerUse = true
userInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.One then
if firstPowerUse == true then
firstPowerUse = false
print("One key was pressed")
--angelInputEvent:FireServer("One", mouse.Hit.Position)
wait(2)
firstPowerUse = true
end
end
end)
Am I missing something? I have a debounce already set up and I don’t think it’s because I’m firing a remote event either because I commented that line out when I was testing out the print line. I’m very confused.
You need to use a :Disconnect() function. Try this:
local connection = nil
function onOneDown(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.One then
if firstPowerUse == true then
firstPowerUse = false
print("One key was pressed")
connection:Disconnect()
--angelInputEvent:FireServer("One", mouse.Hit.Position)
wait(2)
connection = userInputService.InputBegan:Connect(onOneDown)
firstPowerUse = true
end
end
end
userInputService.InputBegan:Connect(onOneDown)
when the event is fired, it disconnects the function when it activates, then it reconnects it when done.