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

How do I make this function to run once?

Help, I notice that when I use ContextActionService to get input, they trigger twice... uh..

Let's say:

local con = game:GetService("ContextActionService")

function hi()
	print("OMG! HI!")
end

con:BindActionToInputTypes("Hi", hi, true, Enum.KeyCode.R)

When I press r, it prints "OMG! HI!" but when I released, it prints too. So, what should I do so that the function can be run when the key is pressed ONLY!

2 comments

0

Use the KeyDown function and add a debounce. Look those 2 up in the wiki.

0

But I need to make it works on mobile too, I use debounce but still, the function runs twice.

1 answer

The third parameter in contextActionService, if set to true, will also call upon the function provided.

local con = game:GetService("ContextActionService")
function hi()
    print("OMG! HI!")
end

con:BindActionToInputTypes("Hi", hi, false, Enum.KeyCode.R)

The above should fix your problem.

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