Hello I'm trying to make a hold-able key ability and it fires twice

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)

What is ok what is a problem about that how can I help you

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.

it also does not print (“done2”) when stopping holding the keybind meaning it does not get to the other part of the script where it stops

Okay come down what happened you wrong

does the script where Im holding the keybind twice and then does not get to the part of the script where I stop holding down the keybind

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)

Go and copy script Here now you have it

Have you used the same script somewhere else?

--Bonnie--
task.spawn(function()

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/...>.

I don’t know what do you think about that

What do you mean, think about what?

That’s going he’s here for me stop holding the keyguy

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.

You’re right because I understand him I don’t know what he said about that

script still prints both the things twice

It’s both like about that think

thank you the script was in somewhere twice