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
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)
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)
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.
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.
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
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)