Press any key to continue (debounce)

Hello,

I am trying to make a UI with a LocalScript that detects when you press a key and goes to another frame.
I have been trying to figure this out for a while. This works but it keeps looping when I press a key.
Here is my code

	game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
		if script.Parent.ReadyForInput.Value == true and script.Parent.Pressed.Value == false then
			script.Parent.Pressed.Value = true
			script.Parent.ReadyForInput.Value = false
			for i = 0.495,-1.5,-0.05 do
				script.Parent.Title.Position = UDim2.new(i, 0, 0.18, 0)
				wait(0.01)
			end
		end
	end)

As you can see I have made 2 bool values to check if the local player has clicked a key and aleady passed this frame.
Thanks for the help

Why not

local debounce = false
game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if not debounce then
        debounce = true
		for i = 0.495,-1.5,-0.05 do
			script.Parent.Title.Position = UDim2.new(i, 0, 0.18, 0)
			wait(0.01)
		end
        debounce = false
	end
end)
1 Like

But I want the ui to come only once and not repeating.

local once = false
game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if not once then
        once = true
		for i = 0.495,-1.5,-0.05 do
			script.Parent.Title.Position = UDim2.new(i, 0, 0.18, 0)
			wait(0.01)
		end
	end
end)

It’s still looping when I press the key few times.

The reason why it keeps loping is because you didn’t disconnect the function using :Disconnect() once its done.
Iit looks like you are trying to make something so as smooth as possible, which I suggest you try looking at TweenService.

1 Like

Wouldn’t TweenService do the same thing?

Because that’s what you gave us, a code that starts a loop on input. What exactly do you want to achieve?

It’s a Menu that detects when you press a key then opens up another frame.

Tweenservice is a much more optimized way to do this, because your way would make the frame very jagged, in addition, TweenService gives you a variety of different animation choices you can chose from.

1 Like

Alright I will try that out
@VegetationBush and @Blokav

Forget my last post, this can be achieved without a BindableEvent since InputBegan is itself an event xD

game:GetService("UserInputService").InputBegan:Wait()

for i = 0.495,-1.5,-0.05 do
	script.Parent.Title.Position = UDim2.new(i, 0, 0.18, 0)
	wait(0.01)
end
1 Like

Why add a bindable event? It isn’t needed. That’s where TweenService can come into some help:

local TS = game:GetService("Tweenservice")
local TI = TweenInfo.new(1)

local Event
Event = game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
    local Animation = TS:Create(script.Parent.Title, TI {Position = where you want the frame to be})
    Animation:Play() -- Plays the animation
    Animation.Completed:Wait() -- Yields the thread(or wait until) the animation is finished playing
    Enent:Disconnect() -- Disconnects the function, so it won't be fired again
end)

@vikenmanoukian21

3 Likes

It’s still looping and I am getting this error.
image

What is on line 106? 30 characters

Position is a Udim2 value. Instead of doing:

{Position = -1.5}

do:

{Position = UDim2.new(-1.5, 0, 0, 0}
2 Likes

I am still getting the same error and the frame is not tweening.

You are missing , between TI and {.

1 Like

The lines of code I write after that do not work. It’s like if the order of lines changed.