Passcode script feedback

Hi, so I am a beginner at Roblox developpement and I created this script that makes a passcode that open a door with a tween if the code is found.
I asked for help in the RSC’s Discord server because there was a bug (that I fixed) and even before helping me they told be that my code was “ass” and the whole channnel basically just roasted me for no reason.
Now I want some feedback about my code and what is really wrong with it if there is somthing wrong with it in the first place.

local code = tostring(math.random(0, 100))

local door = workspace:WaitForChild("Safes_Models").Safe.Parts.Hinge.Door
local hinge	 = workspace:WaitForChild("Safes_Models").Safe.Parts.Hinge
local buttons = workspace:WaitForChild("Safes_Models").Safe.Buttons
local display = workspace:WaitForChild("Safes_Models").Safe.Display.DisplayPart.SurfaceGui
local currentInput = ""

local tweenService = game:GetService("TweenService")
local info = TweenInfo.new(1.5, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out, 0, false, 0)
local open = tweenService:Create(hinge, info, {CFrame = hinge.CFrame * CFrame.Angles(0, math.rad(-120), 0)})

local canWrite = true

for i, v in pairs(buttons:GetChildren()) do
	v.ClickDetector.MouseClick:Connect(function()
		local buttonInput = v.SurfaceGui.TextLabel.Text
		if string.lower(buttonInput) == "+" then
			if currentInput == code then
				display.TextLabel.TextColor3 = Color3.new(0.3, 1, 0)
				display.TextLabel.Text = "Correct"
				open:Play()
				canWrite = false
				task.wait(0.5)
				canWrite = true
				currentInput = ""
				display.TextLabel.TextColor3 = Color3.new(1, 1, 1)
				display.TextLabel.Text = "Code..."
			elseif currentInput ~= code then
				display.TextLabel.TextColor3 = Color3.new(1, 0, 0.01)
				display.TextLabel.Text = "Incorrect"
				canWrite = false
				currentInput = ""
				task.wait(0.5)
				canWrite = true
				display.TextLabel.TextColor3 = Color3.new(1, 1, 1)
				display.TextLabel.Text = "Code..."
			end	
		elseif string.lower(buttonInput) == "-" then
			currentInput = ""
			display.TextLabel.TextColor3 = Color3.new(1, 1, 1)
			display.TextLabel.Text = "Code..."
		else
			if canWrite then
			currentInput = currentInput..buttonInput
			display.TextLabel.Text = currentInput
			end
		end
	end)
end

Thanks for the feedback!

3 Likes

the code is fine aslong as it works intendedly

1 Like

Overall, it looks okay. The thing that’s not necessary is the string.lower(buttoninput) since the - and + dont have uppercase variation, that’s all.

1 Like

btw I even got roasted just because I used ts instead of tweenService

people hate just to hate, simple as

1 Like

Hey, so I took a look at your code. It’s a great start, especially for somebody who’s new to Roblox development. I have a couple suggestions tho. You’re calling workspace:WaitForChild("Safes_Models") multiple times. It’s more efficient to call it once in a variable declaration.

local Safes_Model = workspace:WaitForChild("Safes_Models")
local Safe = Safes_Model.Safe
local door = Safe.Parts.Hinge.Door
local hinge = Safe.Parts.Hinge
local buttons = Safe.Buttons
local display = Safes_Model .Safe.Display.DisplayPart.SurfaceGui

As @mediocrelywarmbeans pointed out, string.lower isn’t necessary since your input strings don’t have an uppercase version. Also, this simplifies your condition checks.

Keep up the good work, and don’t get discouraged by unhelpful comments. Focus on building and learning. Happy coding!

2 Likes

yeah unfortunately that was just pure hate for no reason

I always thought that I’d need to do a WaitForChild everytime. Thanks for the advice!

Well if it works well as intended I do not see any issue with it; however, I do believe the code could be reduced even more for a simpler approach and to prevent any hard reading when look back at the code.

it all depends on the location and time when you load the instances usually since the instances may be loading simulataneously it is simpler to just wait for the first one.