when I press the key it fires twice but if I hold it. it also fires twice. I was wondering if there is any way to only do the script if ur holding down the key for 1 second, here is the script
> local UIS = game:GetService("UserInputService")
> local AbilitiesEvent = game.ReplicatedStorage.Events:WaitForChild("Abilities")
>
> --Bonnie--
> task.spawn(function()
>
> local AdvancedPainInflictionDB = false
> local Mouse = game.Players.LocalPlayer:GetMouse()
>
> local function AdvancedPainInflictionBegan(input, gameProcessed)
> if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.G and Mouse.Target.Parent:FindFirstChild("Humanoid") and not gameProcessed then
> if UIS:IsKeyDown(Enum.KeyCode.G) and AdvancedPainInflictionDB == false then
> print("done")
> AdvancedPainInflictionDB = true
> AbilitiesEvent:FireServer("Bonnie", "AdvancedPainInflict", Mouse.Target.Parent)
> else
> if UIS:IsKeyDown(Enum.KeyCode.G) and AdvancedPainInflictionDB == nil then
> print("cooldown soz bab x")
> end
> end
> if UIS:IsKeyDown(Enum.KeyCode.G) == false and AdvancedPainInflictionDB == true and not gameProcessed then
> print("done2")
> AbilitiesEvent:FireServer("Bonnie", "AdvancedPainInflictStop")
> AdvancedPainInflictionDB = nil
> wait(5)
> AdvancedPainInflictionDB = false
> end
> end
> end
>
> UIS.InputBegan:Connect(AdvancedPainInflictionBegan)
> end)
When I press the key it prints (“done”) twice and also does whatever is in the server script twice. I only want it to execute once, or once I hold the key for no longer than 1 second it then does whatever I want it to do.
local UserInputService = game:GetService("UserInputService")
local keyToDetect = Enum.KeyCode.Space -- Change this to the key you want to detect
local function onKeyPress()
-- Your code to execute when the key is held down
print("Key is held down!")
end
local function onKeyRelease()
-- Your code to execute when the key is released
print("Key is released!")
end
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == keyToDetect then
onKeyPress()
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == keyToDetect then
onKeyRelease()
end
end)
indicates to me that you have assigned this function to a specific character, i.e. “Bonnie”, and may have used it somewhere else, too, e.g. Bob. Since there is no check if “Bonnie” was clicked, it will run times the amount of characters you have assigned it to. You have to make sure to specify who caused the function to run. Since you use and Mouse.Target.Parent:FindFirstChild("Humanoid"), you can add a check there afterwards and <check that Bonnie was selected/clicked/viewed at/...>.
What? The problem is that everything works twice, i.e., the print()-events are done twice etc. which is a clear indicator that this functionality is also used somewhere else and now both events fire at the same time. Atleast that’s how I understand it.