How can I fix this lag after click?

My english is really bad but I still hope you can understand me

I think the title should explain it too.

Hey, I make a random click system, but the problem is: When I try to click it many times it just hang and nothing happens. It means I need to click many times untill anything happens.

Again, it does work but sometimes when I click many times then it just stop out and then I have to wait like 0.5sec untill it does work again.

Here a video for visualize;

https://streamable.com/20csfw

This is the script:

local UserInputService = game:GetService('UserInputService')

local Keys = {

Enum.KeyCode.J,

Enum.KeyCode.H

}

local TextButton = script.Parent.Frame.TextButton

local CurrentKey

local function setRandomKey()

CurrentKey = Keys[math.random(1, #Keys)]

TextButton.Text = string.char(CurrentKey.Value):upper()

end setRandomKey()

UserInputService.InputBegan:Connect(function(input)

if input.KeyCode == CurrentKey then

print('Correct key')

setRandomKey()

else

print('false')

end

end)

I love this community really and I am so happy for getting so much help really thanks ;D!

1 Like
local UserInputService = game:GetService('UserInputService')
local Keys = {"J","H"}
local debounce = true

local TextButton = script.Parent.Frame.TextButton
TextButton.Text = Keys[math.random(#Keys)]

UserInputService.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
	    if input.KeyCode == Enum.KeyCode[TextButton.Text] and debounce then
		    debounce = false
            print('Correct key')
		    TextButton.Text = Keys[math.random(#Keys)]
             debounce = true
	    else	print('false')	end
    end
end)

This is better but it does still lag after many correct keys and false keys. The player should be able to make it correct after He did it false. Try it fast and you will see it lags

Maybe it’s your internet, there are many objects or there are many scripts running because in mine there is no delay.
image

My internet runs at 100k. I have a gaming pc. Just try to fail sometimes and then spam the next. Are you sure you cant optimize this script no more?

I swear its the same as my script it works better at the beginning but sometimes it just lag

https://streamable.com/22mskx

Look this video then maybe you can understand better

Sometimes I just click on H like 5times untill it brings me to J

Because you aren’t checking for duplicates. Your script gets either H or J, sometimes it can get H 5 times in a row.

ohh yes I didnt thought about that

I think it’s because of the initial drop.
With SpawnLocation:
image
Without SpawnLocation:
image
Same delay.

I am sorry for missunderstand and really thanks!

I will put more than 12keys than it shoudnt get this problem anyways thanks!

You might have to change this depending on your script but:

local CurrentKey = "J"
local AvailableKeys = {"J","H"}

repeat
   local NextKey = AvailableKeys[math.random(1, #AvailableKeys)]
until NextKey ~= CurrentKey

My bad I wrote this script before I saw your reply. Well if you ever need to check for duplication, atleast you know how now!

Ok, you’re welcome, happy to help ^ - ^

It would be better if I added an anti debounce.
PS: I edited the script.