Weird logic in a Hide Gui Script

Hi,

So I’m having an Issue with a conditional statement when trying hide a UI Element from the Screen.

When clicking the button, its supposed to set a boolean variable called TopBarHidden to the oppsoite value it was, where a variable called PositionChange will grab a piece of data based on the current status of TopBarHidden.

TopBarHidden = not TopBarHidden; -- true to false and vice versa
local PositionChange = if TopBarHidden then -Top.Position.Y - Top.Size.Y
else Top.Position.Y
-- if true, picks the position where it hides the UI, otehrwise its regular point

The Problem I seem to have with this code is some kind of logic error, because everytime I press the button to hide the UI, it picks the same item twice when it isnt supposed to.

The Boolean works just fine, it switches from true to false upon every click when supposed to.
However when grabbing the data, it instead picks the exact opposite of what the conditional statement is giving until the value is equal to true.

However when spam clicking the button, it works just fine.

When just using a Regular If Statement, the same problem still persists.

	TopBarHidden = not TopBarHidden; -- true to false and vice versa

	local PositionChange -- declare variable

	if TopBarHidden then -- if true
		PositionChange = -Top.Position.Y - Top.Size.Y; -- hidden position
	else -- if not
		PositionChange = Top.Position.Y; -- normal position
	end

What could be causing it?

1 Like

It seems like the issue might be related to the timing of when the TopBarHidden variable is checked and when the UI is updated. If the UI update is happening before the variable check, it could cause the behavior you’re seeing.

One way to address this could be to ensure that the UI update happens after the TopBarHidden variable has been updated.

1 Like

Nope.

TopBarHidden = not TopBarHidden;

	local PositionChange

	if TopBarHidden then
		PositionChange = -Top.Position.Y - Top.Size.Y;
	else
		PositionChange = Top.Position.Y;
	end

	Top:TweenPosition(UDim2.new(UDim.new(.5,0), PositionChange), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, .3, true)

The change is happening right after

12:35:34.285  true  -  Client - MainHandler:112 -- works first time
  12:35:34.285  -0, -43  -  Client - MainHandler:119
  12:35:36.001  false  -  Client - MainHandler:112 -- change here
  12:35:36.002  -0, -43  -  Client - MainHandler:119 -- doesnt work
  12:35:40.035  true  -  Client - MainHandler:112 -- chage here
  12:35:40.035  0, 5  -  Client - MainHandler:119 -- doesnt work

From the logs you’ve sent, it seems like the PositionChange variable is not being updated correctly when TopBarHidden is false. This could be due to the way the Top.Position.Y value is being updated in your script.

One possibility is that the Top.Position.Y value is not updated immediately after the TweenPosition function is called. The TweenPosition function is asynchronous, which means it doesn’t stop the rest of your code from running while it’s moving the UI element. So, when you’re setting PositionChange based on Top.Position.Y right after calling TweenPosition, it might still be getting the old position value.

To fix this, you could try using the TweenFinished event, which fires when a tween operation has completed.

Ended up fixing it by moving around my values to other locations.
It seems to have solved my issue.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.