Why IF Statement Not working?

My Goal?

I am making a time bar for a trivia game and I got the time bar to decrease in width when the question appears :white_check_mark:

My Issue?

Well, I want the script to print ‘‘It Worked!’’ when the timers up.

Start.MouseButton1Click:Connect(function()
	game.Players.LocalPlayer.PlayerGui.ScreenGui["Question 1"].Visible = true
	if Question1.Visible == true then
		timerbar1:TweenSize(
			UDim2.new(0, 0, 0, 30),
			'Out',
			'Quad',
			10)
	end
end)

if timerbar1.Size.X.Offset == 0  then
	print('It worked!')
end

I’ve tried this script but it doesn’t seem to work… :frowning_face: I’ve also reviewed it multiple times but I can’t seem to find the issue :thinking:

Can anybody help me with why my script isn’t working?

You’re checking for the X offset. Change the check to Y offset because your tween changes the Y, not the X

1 Like

image
I want to change the X Offset to 0.

PS: I tried changing it to Y and it still didn’t work.

See if this works:

Start.MouseButton1Click:Connect(function()
	game.Players.LocalPlayer.PlayerGui.ScreenGui["Question 1"].Visible = true
	if Question1.Visible == true then
		timerbar1:TweenSize(
			UDim2.new(0,30, 0,0),
			'Out',
			'Quad',
			10)
	end
end)

if timerbar1.Size.X.Offset == 0  then
	print('It worked!')
end

you need the if statement to trigger after the tween ends
TweenSize doesn’t yield, so in your case, you’re checking the offset during the tween

there’s an example on TweenSize’s Documentation for how to use the Callback

callbacks are functions that trigger after something ends

In Roblox Tween Size Documentation, they said that the last parameter (Callback) runs after the tween has been completed.

So, I tried this-

Start.MouseButton1Click:Connect(function()
	game.Players.LocalPlayer.PlayerGui.ScreenGui["Question 1"].Visible = true
	if Question1.Visible == true then
		timerbar1:TweenSize(
			UDim2.new(0, 0, 0, 30),
			'Out',
			'Quad',
			10,
			false,
			print('It Worked!'))
	end
end)

The problem here is that it’s printing “It Worked!” when the Tween starts instead after it finishes… :frowning_face:

You need to use .Completed. After the tween ends, put that line inside of the function.

you’re calling the function in that case, not passing in the function

.Completed doesnt exist for TweenSize

For his tween, that is. If it even is a normal tween.

it’s GuiObject:TweenSize, not a Tween instance

My mistake. I thought it was a Tween.

an example of calling a function instead of passing in a function is this:

Part.Touched:Connect(print("Part was touched"))

it should be this instead

function someFunction()
   print("Part was touched")
end

Part.Touched:Connect(someFunction)

sup, try this

function finished ()
   print('It worked!')
end

Start.MouseButton1Click:Connect(function()
	game.Players.LocalPlayer.PlayerGui.ScreenGui["Question 1"].Visible = true
	if Question1.Visible == true then
		timerbar1:TweenSize(
			UDim2.new(0, 0, 0, 30),
			'Out',
			'Quad',
			10,
            nil,
            finished)
	end
end)