Delay when a player presses a key to open a UI [SOLVED]

  1. What do you want to achieve?

I need help to fix my script. It is basically when a player presses M on their keyboard a UI will pop up. But in my case, the UI does pop up, but it takes a very long time to do so. It is not instant, which I something I want it to be.

  1. What is the issue?

There is a very long delay, in the UIS.InputBegan code execution.

image

  1. What solutions have you tried so far?

I’ve looked for solutions on Google. But, they did not meet my answer.

UIS.InputBegan:Connect(function(input, gameProgressed)

    if gameProgressed then

        return

    end 

    if input.KeyCode == Enum.KeyCode.M then

        local target = UDim2.new(0.5, 0,0.5, 0) -- Main Panel Position

        -- Intro Text & Image Configs

        local ti = TweenInfo.new(
            1.5, 
            Enum.EasingStyle.Exponential, 
            Enum.EasingDirection.Out, 
            0, 
            false,
            1.5)

        local tween1 = TS:Create(intro1, ti, {Transparency = 1 })
        local tween2 = TS:Create(intro1img, ti, {ImageTransparency = 1 })
        tween1:Play()
        tween2:Play()

        wait(3)
        text.Text = "Starting..."
        wait(.6)  
        text.Text = "Initiating panel...Please wait."
        wait(1)
        text.Text = ""

        do

            -- Main Configs
            local ti2 = TweenInfo.new(
                .5, 
                Enum.EasingStyle.Exponential, 
                Enum.EasingDirection.Out, 
                0, 
                false,
                .5)

            local tween = TS:Create(bg, ti2, { Position = target })
            tween:Play()

            -- Intro Configs

            local ti = TweenInfo.new(
                .6,
                Enum.EasingStyle.Cubic,
                Enum.EasingDirection.In,
                0,
                false,
                .5)

            local twee = TS:Create(intro, ti, {Transparency = 1})
            local twee1 = TS:Create(text, ti, {TextTransparency = 1})
            local twee2 = TS:Create(regiment, ti, {ImageTransparency = 1})
            local twee3 = TS:Create(introtext, ti, {TextTransparency = 1})
            local twee4 = TS:Create(picture, ti, {ImageTransparency = 1})
            local twee5 = TS:Create(pictureborder, ti, {Transparency = 1})
            twee:Play()
            twee1:Play()
            twee2:Play()
            twee3:Play()
            twee4:Play()
            twee5:Play()

        end

        announce.Interactable = true
        revokeBan.Interactable = true
        plrinf.Interactable = true
        moderate.Interactable = true

        print("Category buttons have been activated.")

    end
end)

does it take exactly 1.5 seconds for your ui to finish popping up? did you try messing with the easingstyle?

It isn’t a problem with the tweens, rather my script takes a long time to identify the input. In this case, my local script takes a long time to know that the player has pressed M.

try debugging and place a print() at the beginning right after if input.KeyCode == Enum.KeyCode.M then

Can you fact check this by adding a print statement right after the if statement? If that’s the case, then the print statement should not appear in the output a lot when you spam click M.

image
You can see the delay. “Input Read” is the print() statement, which is placed right after the if statement.

…you do realize you have wait() which delays everything below that script for the given amount of time right?

That wouldn’t affect anything, as when an event is fired, it creates a new thread which runs the function provided. In other words, if an error occurs in an event-connected function, it will not yield the execution of the entire script.

he never used any task.delay() functions or spawn functions so how can you be so sure of that?

Try it by yourself. If an error occurs in a function connected to an event, the entire script should continue, because that error only happens in that specific thread created each time the event gets fired.

Does the print statement outputs multiple times when you spam click M?

Based on his code he never used anything to yield a certain thread without the entire code. If you’re thinking about him spamming the letter “M” to create multiple threads it’s not what I meant. It’s evident that the wait()s are the ones delaying it here

You should add multiple print()s each after every wait() and see.

Thank you for pointing that out. It was indeed the problem, it is fixed.

1 Like

Your issue is the wait()s. Firstly, I believe you should swap these to task.wait() (I have heard these are just easier when it comes to hardware usage).

To fix the gap between the prints and whatnot, I suggest looking into task.spawn.

task.spawn basically, in simple terms, spawns a new script within another script that doesn’t interfere with the original script.

in this case, you can place it where your wait()s are found.

here’s an example of how to use task.spawn:

print"hello world"
task.spawn(function()
	print"hello world from task.spawn"
	task.wait(10)
	print"goodbye world from task.spawn" --this will print 10 seconds after the first one found in the function
end)
print"goodbye world" --this print will print immediately after the first one found in the script in general

If you want it to be faster why is this in the mix …

        wait(3)
        text.Text = "Starting..."
        wait(.6)  
        text.Text = "Initiating panel...Please wait."
        wait(1)

i didn’t know you could actually create print functions without the parentheses :thinking:

It is for UI Design, and has no real purpose. As I said, the problem is fixed, and it works as intended to do so.

Alright. Let me prove it to you that using any functions that yield inside a function that is connected to an event does not pause the entire execution of a script.

I made a part which contains a child script which has this code:

script.Parent.Touched:Connect(function()
	print("HELLO")
	wait(4)
end)

If we are following your theory, the print statement should not print each time the part is touched AFTER it was touched one time. But that is completley false as you can see in this video:


As you can see, I was touching the part multipke times, the wait() function is executed, and, according to your theory, the whole script should pause, without printing out additional outputs until the yielding is done.

That is completely false as you can see in the video. This proves that a new thread is created each time an event is fired.

you can only print raw strings (don’t know if that’s the proper term but basically just “a string like this” that isn’t declared by a variable or anything of the likes)