One thing I discovered only today is that if I put for example a UserInputService.InputBegan inside a function, every time I call that function, the InputBegan is duplicated:
local UserInputService = game:GetService("UserInputService")
test() -- if I include this line, any input will be executed twice...
test() -- here, any keypress will be executed three times... and so on...
Is that a normal behavior?
If yes, how could I “kill” to first InputBegan to avoid this doubled call?
local Input = UserInputService.InputBegan:Wait() -- will only listen once
--same thing but asynchronous ig
connection = UserInputService.InputBegan:Connect(function(Input)
don’t know what you are trying to do but here you go
I have some UserInputService.InputBegan at different parts of my script: one is to be used when a “free camera” is activated; another is to be used when I’m in another section of the game… and so on…
That’s why I’m using it inside functions, to maintain its scopes.
However, this duplication problem is happening when I call the same function twice or more.
Is it possible that you can show the entire script. Connecting InputBegan multiple times isn’t a good idea and I can’t think of any instances where it would be necessary. Perhaps I could modify your script so that it is ‘better’
You shouldn’t have more than one InputBegan events that do the exact same thing, especially if more of them are connected every time the function is called.
What you could try doing is connecting the event when you want it, and disconnecting it when you don’t. Or, if you only want the InputBegan event to fire once then be done, you can :Disconnect() it after it’s fired:
connection = UserInputService.InputBegan:Connect(function(input)