How do I fix this issue with these if statements?

I know the title didn’t explain itself, but the summary will. a lot. I might edit this thread to give you more information but I can’t do it now because I have to go somewhere.

  1. What do you want to achieve? I want to achieve where one LocalScript handles the changes to the taskbar about time. 1 setting if the user wants the midnight system (PM & AM) toggled on/off and other is if the user wants the 24 hour time toggled on/off. (I’m developing kind of like an OS on roblox, basically like ROBLOWS by StarModelX) & the other LocalScript to handle the click events of these 2 settings.

  2. What is the issue? The issue is where both of these if statements (in 1 script) wont work. Even though, it doesn’t print the error to the output. If I remove one of the if statements, it would work for one setting but not both.

  3. What solutions have you tried so far? I have tried separating them into 2 scripts, wont work. I even tried moving them both if statements in different places that I can think of, doesn’t work.

One LocalScript handling the changes: (aka. the script I’m having problems on)

local value1 = script.Parent.Parent.Parent.Settings.Hour24
local value2 = script.Parent.Parent.Parent.Settings.MidnightSystem

script.Parent.Text = os.date("%I:%M %p")

while wait() do
	if value1.Value then
		if value2.Value then
			script.Parent.Text = os.date("%H:%M")
			value2.Value = false
		end

		script.Parent.Text = os.date("%H:%M")
		value1.Value = true
	else
		if not value2.Value then
			script.Parent.Text = os.date("%H:%M") .. os.date("%p")
			value1.Value = true
		end

		script.Parent.Text = os.date("%I:%M %p")
		value1.Value = false
	end
end

Other LocalScript handling the button click events: (aka. the script that I’m NOT having problems on)

local app = script.Parent
local check1 = app.MainPage.TimeSection.MidnightCheck
local usesSystem = true
local value3 = script.Parent.Parent.Parent.Parent.Settings.MidnightSystem

check1.MouseButton1Click:Connect(function()
	if usesSystem then
		check1.Text = ""
		value3.Value = false
		usesSystem = false
	else
		check1.Text = "X"
		value3.Value = true
		usesSystem = true
	end
end)

What specifically are you trying to achieve in the handling changes script?
Also, have you tried putting print statements in every if statement to see where you are in your code?
(you could make the wait() longer for testing purposes)

I’m not sure what your saying but is there a value in Midnight System and Hour 24? Also I’m not sure if I’m correct or not but you never put anything after “then” besides the same thing, but with value2. Maybe you need a statement that goes after “then”?

Are you able to switch both off at the same time? Otherwise I would suggest a different system where you are only able to switch between 24hr and AM/PM for simplicity’s sake.

Either way the while loop is unnecessary and expensive. I would opt for a .Changed event which listens for the change of the bool values you have.

Value.Changed:Connect(function(newvalue)
  if newvalue == true then
    script.Parent.Text = os.date("%H:%M")
  else
    script.Parent.Text = os.date("%I:%M %p")
  end
end)

@LitTurtleMan :

I’m not sure what your saying but is there a value in Midnight System and Hour 24?

Yes, those are boolean values located in the Settings folder in game.StarterGui.OS.Desktop.Settings & I was actually planning to remove the redundant script boolean variables. (local example = false) (^^^ those variables we’re originally used for testing purposes)

Also I’m not sure if I’m correct or not but you never put anything after “then” besides the same thing, but with value2. Maybe you need a statement that goes after “then”?

The Hour24 boolean value is basically like if the user wants to toggle the 24 hour time format or not. But like, what do you mean by like not putting anything after then? do you mean by not putting if statements after another if statement?

@PerilousPanther :

Are you able to switch both off at the same time?

I mean, they’re like an option/setting, I don’t think you can humanly switch switches at the same exact time but when you do switch them both off, It’ll basically lead to the text saying…
for example, it’s 23:31 PM in the evening, and having both off, the text will display something like this:

11:31

Both on:

23:31 PM

24 hour format disabled:
11:31 PM

you know what I mean.

Otherwise I would suggest a different system where you are only able to switch between 24hr and AM/PM for simplicity’s sake. Either way the while loop is unnecessary and expensive. I would opt for a .Changed event which listens for the change of the boolean values you have.

Value.Changed:Connect(function(newvalue)
  if newvalue == true then
    script.Parent.Text = os.date("%H:%M")
  else
    script.Parent.Text = os.date("%I:%M %p")
  end
end)

Okay, thanks. :slight_smile:

Here’s the code for people that have the same problem I had:

local value1 = script.Parent.Parent.Parent.Settings.Hour24
local value2 = script.Parent.Parent.Parent.Settings.MidnightSystem

script.Parent.Text = os.date("%I:%M %p")

value1.Changed:Connect(function(NewValue)
	if NewValue == true then
		if not value2.Value then
			script.Parent.Text = os.date("%H:%M")
		end
	else
		if not value2.Value then
			script.Parent.Text = os.date("%I:%M")
		end
	end
end)

value2.Changed:Connect(function(NewValue)
	if NewValue == false then
		if value1.Value then
			script.Parent.Text = os.date("%H:%M")
		end
		
		script.Parent.Text = os.date("%I:%M")
	else
		if value1.Value then
			script.Parent.Text = os.date("%H:%M %p")
		end
		
		script.Parent.Text = os.date("%I:%M %p")
	end
end)

@xam345 :

Also, have you tried putting print statements in every if statement to see where you are in your code?
(you could make the wait() longer for testing purposes)

Okay, I’ll put print statements to these. Next time.

What specifically are you trying to achieve in the handling changes script?

What I’m specifically trying to achieve in the script that’s handling the changes is where if the user clicked on either, the checkbox (Use Midnight System setting) and/or the button (the 24 hour format setting) it would apply that setting to the taskbar’s clock. (the one that’s in the bottom right of your screen) in my OS.

1 Like

Inside this loop I think it should say value2.Value = true, rather than “value1.Value = true”

1 Like