-
What do you want to achieve?
Help. -
What is the issue?
I’m trying to make a script that can swap UIs intuitively by pressing some buttons. Although, it seems to be really buggy when you spam the buttons, even when it has a debounce. I just can’t wrap my head around it for some reason.
-
What solutions have you tried so far?
A lot of debugging. I’m certain the issue(s?) has to do with theCurrentUI
variable becoming nil (code snippet below), but haven’t been able to fix it.
For context’s sake, the buttons should work like this;
Press a button: UI slides in
Press a button while UI is on the screen: swap UIs
Press the same button the UI corresponds to: UI slides out.
Here’s the corresponding chunk of code that does what I’ve mentioned:
local function TweenIn(UI: UIBase)
UI.Position = UDim2.new(0.7,0,1.5,0)
UI.Visible = true
local movetween = ts:Create(UI, UISwitchTI, {Position = UDim2.new(0.7,0,0.5,0)})
movetween:Play() -- originally used :TweenPosition() but it resulted to bug out so I opted for this
task.wait(0.55) -- 0.55 is exactly the tween's time btw
CurrentUI = UI
CanPressButton = true
return
end
local function TweenOut(UI: UIBase, perm: boolean)
local movetween = ts:Create(UI, UISwitchTI, {Position = UDim2.new(CurrentUI.Position.X.Scale, CurrentUI.Position.X.Offset, -0.5,0)})
movetween:Play()
task.wait(0.55)
UI.Visible = false
if perm == true then
CurrentUI = nil
end
CanPressButton = true
return
end
local function IntroduceUI(NewUI: GuiObject)
if CanPressButton == true then
CanPressButton = false
if CurrentUI == nil then -- if there are no UIs on screen, swipe one up
warn(CurrentUI)
TweenIn(NewUI)
elseif CurrentUI == NewUI then -- if the button is the same as the current UI, swipe it ou
TweenOut(CurrentUI, true)
else -- if there are two UIs, swap them
TweenOut(CurrentUI)
TweenIn(NewUI)
end
print(CurrentUI)
end
end
CanPressButton
acts as a debounce btw
Any help is appreciated! This is my second topic, so feedback on details would be nice too