How would I make this script 'less-buggy'?

Okay so, I’ve just made a script that uses UserInputService and TweenService. There are quite a few common bugs, such as the letterBox not tweening away, or the blur not going, this tend to happen if I press the required key to many times.

local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quint)

local SettingsKey = Enum.KeyCode.Q

local blur = game.Lighting:WaitForChild("Blur")

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == SettingsKey then
			if script.Parent.Parent.Name == game.Players.LocalPlayer.Name then
				script.Parent.Parent.PlayerGui.LetterBox.L2:TweenPosition(UDim2.new(0, 0,0.882, 0),"Out","Quart",0.5,false)
				script.Parent.Parent.PlayerGui.LetterBox.L1:TweenPosition(UDim2.new(0, 0,0, 0),"Out","Quart",0.5,false)
				TS:Create(blur, tweenInfo, {Size = 16}):Play()
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == SettingsKey then
			if script.Parent.Parent.Name == game.Players.LocalPlayer.Name then
				script.Parent.Parent.PlayerGui.LetterBox.L2:TweenPosition(UDim2.new(0, 0,1, 0),"Out","Quart",0.5,false)
				script.Parent.Parent.PlayerGui.LetterBox.L1:TweenPosition(UDim2.new(0, 0,-0.15, 0),"Out","Quart",0.5,false)
				TS:Create(blur, tweenInfo, {Size = 0}):Play()
			end
		end
	end
end)

Thanks. :slight_smile:

Add a debounce. Maybe it can solve this issue.

How would I go about adding a debounce here?

local db = false
local cooldown = 1

UserInputService.InputBegan:Connect(function(input, GPE)
    if not db then
        db = true
        — do your stuffs here.
        wait(cooldown)
        db = false
    end
end)
2 Likes