Safe Scripting problem

Hi I am making a safe with this script

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

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

local currentInput = ""

local ts = game:GetService("TweenService")

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

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()
				wait(1)
				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"
				currentInput = ""
				wait(1)
				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
			
			currentInput = currentInput..buttonInput
			display.TextLabel.Text = currentInput
		
		end
	end)
end

Unfortunately I had this problem where I can still write after the result how can I fix this ?

1 Like

Try adding a canWrite variable and add symbols only when its true

I’m trying but It’s not working

Could you provide code with the variable?

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

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

local currentInput = ""

local ts = game:GetService("TweenService")

local info = TweenInfo.new(1.5, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out, 0, false, 0)
local open = ts: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"
				canWrite = false
				open:Play()
				wait(1)
				currentInput = ""
				display.TextLabel.TextColor3 = Color3.new(1, 1, 1)
				display.TextLabel.Text = "Code..."
				wait(0.5)
				canWrite = true
					
			elseif currentInput ~= code then
				display.TextLabel.TextColor3 = Color3.new(1, 0, 0.01)
				display.TextLabel.Text = "Incorrect"
				canWrite = false
				currentInput = ""
				wait(1)
				display.TextLabel.TextColor3 = Color3.new(1, 1, 1)
				display.TextLabel.Text = "Code..."
				task.wait(0.5)
				canWrite = true
			end	
			
		elseif string.lower(buttonInput) == "-" then
			
			currentInput = ""
			display.TextLabel.TextColor3 = Color3.new(1, 1, 1)
			display.TextLabel.Text = "Code..."
		else
			
			currentInput = currentInput..buttonInput
			display.TextLabel.Text = currentInput
		
		end
	end)
end

I set the variable to false when the Correct or Incorrect is there and set to true when I can write

Ok i a bit modified your code, try this:

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

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

local ts = game:GetService("TweenService")

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

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()
canAdd = false
				task.wait(1)
canAdd = 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"
				currentInput = ""
canAdd = false
				task.wait(1)
canAdd = 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 canAdd then
			currentInput = currentInput..buttonInput
			display.TextLabel.Text = currentInput
		end
		end
	end)
end

I think you forgor to add a check if canWrite variable is true

local code = tostring(math.random(0, 5))
local door = workspace:WaitForChild(“Safe”).Parts.Hinge.Door
local hinge = workspace:WaitForChild(“Safe”).Parts.Hinge
local buttons = workspace:WaitForChild(“Safe”).Buttons
local display = workspace:WaitForChild(“Safe”).Display.DisplayPart.SurfaceGui

local currentInput = “”
local isBusy = false
local isSolved = false

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

local function setDisplay(text, color)
display.TextLabel.Text = text
if color then display.TextLabel.TextColor3 = color end
end

for _, v in ipairs(buttons:GetChildren()) do
local cd = v:FindFirstChild(“ClickDetector”)
local gui = v:FindFirstChild(“SurfaceGui”)
local label = gui and gui:FindFirstChild(“TextLabel”)
if cd and label then
cd.MouseClick:Connect(function()
if isSolved or isBusy then return end – block input

		local buttonInput = label.Text

		if buttonInput == "+" then
			isBusy = true

			if currentInput == code then
				setDisplay("Correct", Color3.new(0.3, 1, 0))
				open:Play()
				isSolved = true
				
				-- optionally hard-disable buttons:
				for _, b in ipairs(buttons:GetChildren()) do
					local bcd = b:FindFirstChild("ClickDetector")
					if bcd then bcd.MaxActivationDistance = 0 end
				end
			else
				setDisplay("Incorrect", Color3.new(1, 0, 0.01))
				currentInput = ""
				task.delay(1, function()
					if not isSolved then
						setDisplay("Code...", Color3.new(1, 1, 1))
						isBusy = false
					end
				end)
			end

		elseif buttonInput == "-" then
			currentInput = ""
			setDisplay("Code...", Color3.new(1, 1, 1))

		else
			-- optional: cap input length
			if #currentInput < 6 then
				currentInput ..= buttonInput
				setDisplay(currentInput)
			end
		end
	end)
end

end
Try this script I did adjust extra parts of it to be a little cleaner and safe the problem I think you needed a Input lock tell me if it works you should be able to just drop it in

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.