Hello! I am trying to make a game simulating a computer operating system, and I have issues with the shutdown system I have in place. Script:
local OS = game.Players.LocalPlayer.PlayerGui.OS
local ShutdownScreen = game.Players.LocalPlayer.PlayerGui.ShutdownScreen
local ShutdownSound = game.Players.LocalPlayer.PlayerGui.OS.System_Sounds.Shutdown
local StartMenu = game.Players.LocalPlayer.PlayerGui.OS.Start_Menu
local ShutdownStatus = ShutdownScreen.Frame.Notice
script.Parent.MouseButton1Click:Connect(function()
ShutdownSound:Play()
StartMenu.Visible = false
task.wait(3.944)
OS.Enabled = false
ShutdownScreen.Enabled = true
print("Shutting down PC")
for i = 1,1123,1 do
task.wait(0.001)
ShutdownStatus.Text = "Shutting down logon.exe ("..i.."/1123)"
if i >= 112 then
ShutdownStatus.Text = "Shutting down 30con.exe ("..i.."/1123)"
elseif i >= 354 then
ShutdownStatus.Text = "Shutting down explorer.exe ("..i.."/1123)"
elseif i >= 453 then
ShutdownStatus.Text = "Shutting down Service Hosts ("..i.."/1123)"
elseif i >= 789 then
ShutdownStatus.Text = "Shutting down OS Processes ("..i.."/1123)"
elseif i >= 834 then
ShutdownStatus.Text = "Shutting down DOS ("..i.."/1123)"
end
end
ShutdownStatus.Text = "PC has been shut down."
wait(0.5)
ShutdownStatus.Text = "Type \"!rejoin\" to restart PC."
end)
Problem:
The problem occurs during the if statement inside of the for loop on line 17. ShutdownStatus.Text changes to “Shutting down 30con.exe (”…i…“/1123)”, as expected. But, when ‘i’ >= 354, ShutdownStatus.Text does not change to “Shutting down explorer.exe (”…i…“/1123)”. The same issue with the elseif statements not working repeats again and again until the end of the for loop, when ‘i’ finally reaches 1123.
local Number = 5
if Number > 1 then
print("Number is greater than 1")
elseif Number > 2 then
print("Number is greater than 2")
end
-- Number is greater than 1 so it doesn't check the elseif blocks
--> Number is greater than 1
local Number = 5
if Number > 1 then
print("Number is greater than 1")
end
if Number > 2 then
print("Number is greater than 2")
end
-- Number is greater than 1 so it prints, and moves to the next if statement
--> Number is greater than 1
--> Number is greater than 2
elseif only happens if the condition above isn’t true
if the number is 112 then the first condition is met i >= 112
if the number is 354 then the first condition is met i >= 112
since the first condition is always true for numbers 112 and up, the other blocks will never be reached
so either check backwards or separate the if statements
You don’t understand how elseif statements work. It will run code block 1 if the first expression resolves to true, but if not the second, and if not the third, until the end or there is an else block.
The best way to solve this is either check backwards as @BonesIsUseless stated, or use separate if statements if this is the behavior you’re looking for.
local OS = game.Players.LocalPlayer.PlayerGui.OS
local ShutdownScreen = game.Players.LocalPlayer.PlayerGui.ShutdownScreen
local ShutdownSound = game.Players.LocalPlayer.PlayerGui.OS.System_Sounds.Shutdown
local StartMenu = game.Players.LocalPlayer.PlayerGui.OS.Start_Menu
local ShutdownStatus = ShutdownScreen.Frame.Notice
script.Parent.MouseButton1Click:Connect(function()
ShutdownSound:Play()
StartMenu.Visible = false
task.wait(3.944)
OS.Enabled = false
ShutdownScreen.Enabled = true
print("Shutting down PC")
for i = 1,1123,1 do
task.wait(0.001)
ShutdownStatus.Text = "Shutting down logon.exe ("..i.."/1123)"
if i >= 112 then ShutdownStatus.Text = "Shutting down 30con.exe ("..i.."/1123)" end
if i >= 354 then ShutdownStatus.Text = "Shutting down explorer.exe ("..i.."/1123)" end
if i >= 453 then ShutdownStatus.Text = "Shutting down Service Hosts ("..i.."/1123)" end
if i >= 789 then ShutdownStatus.Text = "Shutting down OS Processes ("..i.."/1123)" end
if i >= 834 then ShutdownStatus.Text = "Shutting down DOS ("..i.."/1123)" end
end
ShutdownStatus.Text = "PC has been shut down."
wait(0.5)
ShutdownStatus.Text = "Type \"!rejoin\" to restart PC."
end)
The issue is that multiple of your if statements can be true at once and they’re in the reverse order of your expected priority logic. For example when i >= 354 is true, i >= 112 is also true, and its evaluated first(because you put it before the 354 case). So i >= 112 will run instead.
To fix the issue you just need to reverse the priority of the if-statemets, so:
if i >= 834 then
--case 5
elseif i >= 789 then
--case 4
elseif i >= 453 then
--case 3
elseif i >= 354 then
--case 2
elseif i >= 112 then
--case 1
end
Or add more restrictions on your original if statements, for example if i >= 112 and i < 354
In general when multiple cases within your if-statement are true, the one mentioned first will be executed(in your case i >= 112).
if i >= 834 then
-- your code here
elseif i >= 789 then
-- your code here
elseif i >= 453 then
-- your code here
elseif i >= 354 then
-- your code here
elseif i >= 112 then
-- your code here
end
I didn’t see this comment, to add on to this, apply the same logic to the first code as well OP. Your elseif statements should be descending, not just in this situation but in almost all.