I’m making a script to open a Gui with TweenService, using the button it worked without bugs However using the UserInputService I noticed that if the player repeatedly clicks the selected button the Gui ends up crashing. Can someone help me? follow my code below.
-- Services
local TweenService = game:GetService("TweenService")
-- Positions
local InventoryOpen = UDim2.new(0.323, 0,0.540, 0, 0)
local InventoryClose = UDim2.new(0.323, 0,1.5, 0)
-- Gui
local Gui = script.Parent; Gui.Enabled = true
local openButton = Gui.OpenButton
local closeButton = Gui.OpenButton
local InventoryFrame = Gui.Inventory; InventoryFrame.Position = InventoryClose;
InventoryFrame.Visible = false
-- Open/Close Function
local function OpenInventory()
if (not InventoryFrame.Position) == InventoryClose then return end
InventoryFrame.Visible = true
local Info =
TweenInfo.new (1,
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out,
0,false,0)
local InventoryPos = {Position = InventoryOpen;}
TweenService:Create(InventoryFrame, Info, InventoryPos):Play()
end
local function CloseInventory()
if (not InventoryFrame.Position) == InventoryOpen then return end
local Info =
TweenInfo.new(1,
Enum.EasingStyle.Sine,
Enum.EasingDirection.In,
0,false,0)
local InventoryPos = {Position = InventoryClose;}
TweenService:Create(InventoryFrame, Info, InventoryPos):Play()
wait(1)
InventoryFrame.Visible = false
end
-- Connecting Buttons
openButton.MouseButton1Click:Connect(function()
if InventoryFrame.Position == InventoryOpen then
CloseInventory()
elseif InventoryFrame.Position == InventoryClose then
OpenInventory()
end
end)
closeButton.MouseButton1Click:Connect(CloseInventory)
Now is the part that I have doubts, how do I avoid the bugs of pressing repeatedly
game:GetService("UserInputService").InputBegan:Connect(function(input,gameProcessedEvent)
if gameProcessedEvent then return end -- Prevents this from running when typing in chat
if input.KeyCode == Enum.KeyCode.Q then
if InventoryFrame.Position == InventoryOpen then
CloseInventory()
else
OpenInventory()
end
end
end)