Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with ContextActionService?

Hey, guys so I am having a bit of an issue with ContextActionService. I am trying to make it so that if I hold down a certain key on the keyboard a function keeps running however, when I stop holding it down and let go the function stops working. So now, basically I have tried a lot of this and have worked on it for quiet a long time now. However, I can't grasp as to how I will make this work in the best way that will benefit my game. So the script I have tried to do that with is right here:

local deb = true
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

function onQ(name,state,action)
    if state == Enum.UserInputState.Begin then
	while deb == true do
		print("ONQ")
		wait(.5)
	end
	elseif state == Enum.UserInputState.End then
		deb = false
		print("OFFQ")
		deb = true
	end
	end
	
game.ContextActionService:BindAction("OnQ", onQ, true, Enum.KeyCode.Q)

As you can see above I tried to use a Debounce and I think that's what will help me do it since that's what I used with KeyDown and KeyUp however, this script isn't working exactly how I want it to work.

Any help is appreciated and thank you a lot for reading my question and attempting to help me out.

~~KingLoneCat

1 answer

✓ Accepted answer
local q_down = false --I'm not a big fan of calling this "debounce", but call it what you'd like.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

function onQ(name,state,action)
    if state == Enum.UserInputState.Begin then --When you press q
		q_down = true --Q is now down
    	while q_down == true do --Do this stuff while q is down
        	print("ONQ")
        	wait(.5)
    	end
    elseif state == Enum.UserInputState.End then --When you stop pressing q
        q_down = false --Break the loop above
        print("OFFQ")
    end
end

game.ContextActionService:BindAction("OnQ", onQ, true, Enum.KeyCode.Q)

This discussion is preserved as it was. New answers, comments, and votes are closed.