Help with UI swapping

  1. What do you want to achieve?
    Help.

  2. 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.

  1. What solutions have you tried so far?
    A lot of debugging. I’m certain the issue(s?) has to do with the CurrentUI 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 :smiley:

i think its because the script is waiting for one function to finish because of the wait. just make this change so it can continue without waiting for each tween to finish. it should be a lot smoother:

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
	
	delay(0.55, function() -- 0.55 is exactly the tween's time btw
		CurrentUI = UI
		CanPressButton = true
		return
	end)
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()
	
	delay(0.55, function()
		UI.Visible = false
		if perm	== true then
			CurrentUI = nil
		end
		CanPressButton = true
		return
	end)
end

1 Like

Heyy, it works! Thank you so much, I’ve been banging my head against a wall for way too long with this.

no problem! have fun making games!

1 Like

I know the issue has been resolved, but I want to compliment how good this menu looks.

Good job.

1 Like