Reroll my Door's Passcode

I’ve been trying to make it so that after the player inputs a code, be it correct or incorrect, the code for the door will be randomized again. I’ve tried multiple things; attributes, module scripts, booleans, if-statements and the like. However, they only work partially* or end up failing (*partially meaning it’ll run but it’ll overlap or the script will get messed up and just keep printing incorrect on the display).

Does anyone have any ideas on how I’m supposed to do this? Or do I need to rewrite the script itself?

local Code = math.random(1111,9999)
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Door = workspace:WaitForChild("KeypadDoor").Parts.Door
local Buttons = workspace:WaitForChild("KeypadDoor").Buttons
local InfoParts = workspace:WaitForChild("KeypadDoor").InfoParts
local Display = workspace:WaitForChild("KeypadDoor").Display.Display.Hint
local CurrentInput = ""

for i,v in pairs(InfoParts:GetChildren()) do
	local CodeString = tostring(Code)
	InfoParts[tostring(i)].Hint.Code.TextLabel.Text = string.sub(CodeString,i,i)
	RunService.Stepped:Wait()
end

local OriginalPosition = Door.CFrame
local OpenPosition = Door.CFrame * CFrame.new(-4.5,0,0)
local OpenGoal = {}
local CloseGoal = {}
OpenGoal.CFrame = OpenPosition
CloseGoal.CFrame = OriginalPosition
local Info = TweenInfo.new(1, Enum.EasingStyle.Bounce)
local DoorOpen = TweenService:Create(Door,Info,OpenGoal)
local DoorClose = TweenService:Create(Door,Info,CloseGoal)

for i,v in pairs(Buttons:GetChildren()) do
	v.ClickDetector.MouseClick:Connect(function()
		local OriginalCFrame = v.CFrame
		local ButtonInput = v.Hint.TextLabel.Text
		v.CFrame = v.CFrame * CFrame.new(0,0,-0.1)
		for i=1,10 do
			RunService.Stepped:Wait()
		end
		v.CFrame = OriginalCFrame
		if string.lower(ButtonInput) == "enter" then
			if CurrentInput == tostring(Code) then
				Display.TextLabel.Text = "CORRECT"
				CurrentInput = ""
				DoorOpen:Play()
				for i=1,360 do
					RunService.Stepped:Wait()
				end
				Display.TextLabel.Text = ""
				DoorClose:Play()
			elseif CurrentInput ~= tostring(Code) then
				Display.TextLabel.TextColor3 = Color3.fromRGB(255, 95, 84)
				Display.TextLabel.Text = "INCORRECT"
				CurrentInput = ""
				for i=1,60 do
					RunService.Stepped:Wait()
				end
				Display.TextLabel.Text = ""
				Display.TextLabel.TextColor3 = Color3.fromRGB(85, 255, 127)
			end
		elseif string.len(CurrentInput) < 4 then
			CurrentInput = CurrentInput..ButtonInput
			Display.TextLabel.Text = CurrentInput
		end
	end)
end
1 Like

I honestly think you’re doing a bit over for what you need to. Take for example a chat message,

If I chat the correct code in chat,

We’ll say the code for now is 111

local Code = 111

Then if msg == Code then you can change the code value with a math.random.

local Code = 1111

game.Players.PlayerAdded:connect(function(plr)
   plr.Chatted:connect(function(msg)
      if msg == Code then
         Code = math.random(1, 9999) --It changes it to a random number
      end
   end)
end)

So check if the password is correct, and if it is, change the password/code

As @benpinpop mentioned, from what I can see in your code, you are not randomizing after an attempt was made to input the code. So you have to do that like how you did it at first to initialize the variable. And from the looks of it, the variable is used for something else, so if you want the Hint code to work when the code is updated, you should turn it into a function instead.

local function UpdateCode()
   for i,v in pairs(InfoParts:GetChildren()) do
       Code = math.random(1111,9999)
	   local CodeString = tostring(Code)
	   InfoParts[tostring(i)].Hint.Code.TextLabel.Text = string.sub(CodeString,i,i)
   	   RunService.Stepped:Wait()
   end
end
UpdateCode()

I placed the function at the end of declaring it so it will run once before anything else

Then at the end of the code that does stuff for the codes, you would do, in the case of guessing it wrong for example

elseif CurrentInput ~= tostring(Code) then
	Display.TextLabel.TextColor3 = Color3.fromRGB(255, 95, 84)
	Display.TextLabel.Text = "INCORRECT"
	CurrentInput = ""
	for i=1,60 do
	   RunService.Stepped:Wait()
	end
	Display.TextLabel.Text = ""
	Display.TextLabel.TextColor3 = Color3.fromRGB(85, 255, 127)
    UpdateCode()
end

This code will, at the end of doing the stuff when guessing it wrong, will randomize the code and update the hint stuff since it’s in that function. Hopefully my theory will be of help

Well it would work but the if-statement’s inputs also use “tostring(Code)” so if I placed the “Code = math.random(1111,9999)” variable inside a function then the if-statement can’t find that variable.

The Code variable is already declared in the script, what the function does is edits the contents of the variable, the statements can still access it

Oh nevermind, I thought you wrote “Local Code =” but it was just “Code =”

It’s fine, if you want you can try out the code changes I’ve shown to see if it works, which it hopefully should. Good luck!

1 Like

Well, it rerolls the code, but even if I input the correct code it says incorrect and rerolls it.

Script:

local Code = nil
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Door = workspace:WaitForChild("KeypadDoor").Parts.Door
local Buttons = workspace:WaitForChild("KeypadDoor").Buttons
local InfoParts = workspace:WaitForChild("KeypadDoor").InfoParts
local Display = workspace:WaitForChild("KeypadDoor").Display.Display.Hint
local CurrentInput = ""

local function UpdateCode()
	for i,v in pairs(InfoParts:GetChildren()) do
		Code = math.random(1111,9999)
		local CodeString = tostring(Code)
		InfoParts[tostring(i)].Hint.Code.TextLabel.Text = string.sub(CodeString,i,i)
		RunService.Stepped:Wait()
	end
end

UpdateCode()

local OriginalPosition = Door.CFrame
local OpenPosition = Door.CFrame * CFrame.new(-4.5,0,0)
local OpenGoal = {}
local CloseGoal = {}
OpenGoal.CFrame = OpenPosition
CloseGoal.CFrame = OriginalPosition
local Info = TweenInfo.new(1, Enum.EasingStyle.Bounce)
local DoorOpen = TweenService:Create(Door,Info,OpenGoal)
local DoorClose = TweenService:Create(Door,Info,CloseGoal)

for i,v in pairs(Buttons:GetChildren()) do
	v.ClickDetector.MouseClick:Connect(function()
		local OriginalCFrame = v.CFrame
		local ButtonInput = v.Hint.TextLabel.Text
		v.CFrame = v.CFrame * CFrame.new(0,0,-0.1)
		for i=1,10 do
			RunService.Stepped:Wait()
		end
		v.CFrame = OriginalCFrame
		if string.lower(ButtonInput) == "enter" then
			if CurrentInput == tostring(Code) then
				Display.TextLabel.Text = "CORRECT"
				CurrentInput = ""
				DoorOpen:Play()
				for i=1,360 do
					RunService.Stepped:Wait()
				end
				Display.TextLabel.Text = ""
				DoorClose:Play()
				UpdateCode()
			elseif CurrentInput ~= tostring(Code) then
				Display.TextLabel.TextColor3 = Color3.fromRGB(255, 95, 84)
				Display.TextLabel.Text = "INCORRECT"
				CurrentInput = ""
				for i=1,60 do
					RunService.Stepped:Wait()
				end
				Display.TextLabel.Text = ""
				Display.TextLabel.TextColor3 = Color3.fromRGB(85, 255, 127)
				UpdateCode()
			end
		elseif string.len(CurrentInput) < 4 then
			CurrentInput = CurrentInput..ButtonInput
			Display.TextLabel.Text = CurrentInput
		end
	end)
end

Video: https://streamable.com/5u23ih

It could be because of CurrentInput. Try printing the value of CurrentInput by placing print(CurrentInput, Code) anywhere inside of the MouseClick Event. This will print both CurrentInput and Code, spaced out of course

This… seems wrong…

https://streamable.com/lxjwvo

The actual code and the one displayed on the hints are different for some reason?

I realised the issue, change the function to

local function UpdateCode()
    Code = math.random(1111,9999)
	for i,v in pairs(InfoParts:GetChildren()) do
		local CodeString = tostring(Code)
		InfoParts[tostring(i)].Hint.Code.TextLabel.Text = string.sub(CodeString,i,i)
		RunService.Stepped:Wait()
	end
end

I accidentalyl put the code that changes the Code variable inside of the loop, it was being changed 4 times which messed up the keypad

1 Like

Ohhhhhh. Thanks so much for the help! I really appreciate it. (I tried doing a function earlier but it seems I was putting too much of the code into it compared to this one :sweat_smile:)

Anytime! Glad to have been help to you! If you need anymore help, don’t be afraid to make another post!