Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make this function to run once?

Asked by
Jash50 5
9 years ago

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!

0
Use the KeyDown function and add a debounce. Look those 2 up in the wiki. EzraNehemiah_TF2 3552 — 9y
0
But I need to make it works on mobile too, I use debounce but still, the function runs twice. Jash50 5 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

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.

Ad

Answer this question