hello guys! i need your guy’s help to improve my microwave, or any of those bugs that need to be crushed
I’m always striving for improvements for my script, it makes me feel better when i finally crushed that annoying bug.
first, let me explain what im trying to do
I’m am currently working on making a working microwave with no bugs as possible and also trying to look for improvements on the code.
now let me how my creation does/works
well i have made a microwave, and it works
i haven’t polished it nor does it have much features except “cooking” and pressing buttons (also closing door)
Number Pad Buttons
yes these are buttons alright
0-9 on the microwave
here’s the script for it
for i,button in pairs(buttons:GetChildren()) do
local value = tonumber(button.Name) --value of button is determined by the name of it
local textButton = button.SurfaceGui.TextButton --gets the text button
if textButton then --if text button then
textButton.MouseButton1Click:Connect(function() -- when the button is pressed
cookingConnect:Disconnect() -- disconnects the cooking for microwave
if Input == 0 and value == 0 then return end -- can't input the value 0 into the microwave if there is no value
if string.len(Input) == 4 then -- most microwave timers dont extent to 5 numbers, just 4 such as 12:34
Input = 0 --makes input 0
end
Input = tonumber(Input..value)--adds the current input and the button value
displayTime(Input) -- then it updates the text label to the new input
end)
end
end
well the original i didn’t used to have a for loop for each of the buttons so i just had to create them each by just coping and pasting the same function
Getting the Time
now, how do we cook the food if we don’t have the time for it
well i got something, well if the player press any of the number buttons it adds to the input
if the input is 1234 then the function separates the numbers left of the colon into minutes, and it’ll be 12 minutes, and the numbers right of the colon will be seconds and it’ll be 34 seconds
and here’s the function
local function convertTime(Time)
minutes = tonumber("0"..string.sub(Time, 1,-3)) -- gets the minutes of the time if the parameter of Time = 1234 then minutes will be 12
seconds = tonumber(string.sub(Time, -2)) -- gets the seconds of the time if the parameter of Time = 1234 then seconds will be 34
return string.format("%02i:%02i",minutes,seconds) -- then here's the formatting it does the hardwork for me so if minutes and seconds are 12 and 34 it'll format them as normal
--but if the seconds is 1 and minutes are 1 then without the 02 in the string format the time would look something like this 1:1 and it looks off but with the string format makes it look like 01:01
--just like a microwave timer!
end
simple? i know right, it didn’t always was this simple, i had no clue about strings when i first tried creating it and it looked like this monstrosity
local function getTime()
if Input.Value < 10 then
minutes = 0
seconds = Input.Value
SpinTime = minutes..":".."0"..seconds
print(SpinTime)
elseif Input.Value < 100 then
minutes = 0
seconds = Input.Value
SpinTime = minutes..":"..seconds
print(SpinTime)
elseif Input.Value < 1000 then
minutes = tonumber(string.sub(tostring(Input.Value), 0,1))
seconds = tonumber(string.sub(tostring(Input.Value), 2))
if seconds < 10 then
SpinTime = minutes..":".."0"..seconds
print(SpinTime)
else
SpinTime = minutes..":"..seconds
print(SpinTime)
end
elseif Input.Value < 10000 then
minutes = tonumber(string.sub(tostring(Input.Value), 0,2))
seconds = tonumber(string.sub(tostring(Input.Value), 3))
if seconds < 10 then
SpinTime = minutes..":".."0"..seconds
print(SpinTime)
else
SpinTime = minutes..":"..seconds
print(SpinTime)
end
end
end
ahh its hideous, it has so many if statements and it looks like a big mess
Cooking
I’ve made a conventional while loop using runserivce heartbeat, im not too sure if its actually good or not but at least its noticeably better than a regular while loop when i was using it
local function cook()
if cooldown then return end -- cool down is for using run service or else without it its gonna be done faster than you can clap your hands 1000000 times
cooldown = true
if minutes > 0 and seconds <= 0 then --if there are still minutes left, but seconds is 0 then it subtracts a minute and makes seconds to 59
minutes -= 1
seconds = 59
else
seconds -= 1 -- else it just subtracts a second
end
Input = tonumber(string.format("%02i%02i",minutes,seconds))--updates the input so if the timer stops it can resume the same time as it was stopped at
displayTime(Input) --updates the display with the new input
task.wait(1) --waits a second
cooldown = false
if minutes <= 0 and seconds <= 0 then cookingConnect:Disconnect() Input = 0 return end --checks if both minutes and seconds is 0 then it stops cooking
end
and yeah i tried to make it the best i could, but im pretty sure that i could improve
also here’s my old one, i had no idea what i was doing while making it
local function cook()
local textLabel = screen:FindFirstChildOfClass("SurfaceGui"):FindFirstChildOfClass("TextLabel")
Cooking = true
getTime()
--plate move--
plate()
while Opened == false and Cooking == true do
if Opened == true or Cooking == false then
Cooking = false
--plate stop--
plate()
break
end
if seconds <= 0 then
minutes = minutes - 1
seconds = 59
else
seconds = seconds - 1
end
if minutes == 0 and seconds == 0 then
Cooking = false
Input.Value = 0
textLabel.Text = "00:00"
--plate stop--
plate()
break
else
if minutes >= 10 and seconds >= 10 then
textLabel.Text = tostring(minutes)..":"..tostring(seconds)
elseif minutes >= 10 and seconds < 10 then
textLabel.Text = tostring(minutes)..":".."0"..tostring(seconds)
elseif minutes < 10 and seconds >= 10 then
textLabel.Text = "0"..tostring(minutes)..":"..tostring(seconds)
elseif minutes < 10 and seconds < 10 then
textLabel.Text = "0"..tostring(minutes)..":".."0"..tostring(seconds)
end
end
wait(1)
if Opened == true or Cooking == false then
Cooking = false
--plate stop
plate()
break
end
end
end
its awful its too lenghty and confusing
and thats pretty much it for the main functions
Other Buttons
-
first there is an enter button, the only way to make the microwave to cook is to press that button and only works if the door of the microwave is closed and if the input isn’t 0
-
second there is a clear button, it clears the input by making it 0
-
and third there is a door(its on the side btw) when the wave is cooking it stops it when door is open other wise it just closes the door, and there is currently an indicator to tell when the door is open(green) and closed(red)
also if you want you can compare each of them for a side by side comparison
or check the source code of either
here ya go
old:
oldmicrowave.rbxm (13.5 KB)
new:
newmicrowave.rbxm (11.4 KB)
and thats it for the code review, hope you have a good day!