Random combination code instantly right

I’m trying to make a combination code system with random values, but sometimes the code is instantly correct and the door opens immediately, which i don’t want to happen.

Script:

local gate = script.Parent.Gate
local code = nil
repeat until code ~= 0000 do
	code = (math.random(0,1)..math.random(0,1)..math.random(0,1)..math.random(0,1))
end
local ts = game:GetService("TweenService")
local gateinfo = TweenInfo.new(2.12,Enum.EasingStyle.Sine)
local gatepos = gate.Position
print(code)

local valve1 = script.Parent.Valve1
local valve2 = script.Parent.Valve2
local valve3 = script.Parent.Valve3
local valve4 = script.Parent.Valve4

local solved = false

while wait(0.1) do
	if solved == false then
		local plrcode = valve1.Value.Value..valve2.Value.Value..valve3.Value.Value..valve4.Value.Value
		print(plrcode)
		if plrcode == code then
			solved = true
			valve1.Valve.Attachment.ProximityPrompt.Enabled = false
			valve2.Valve.Attachment.ProximityPrompt.Enabled = false
			valve3.Valve.Attachment.ProximityPrompt.Enabled = false
			valve4.Valve.Attachment.ProximityPrompt.Enabled = false
			script.Parent.Light.Color = Color3.fromRGB(0, 255, 0)
			script.Parent.Light.PointLight.Color = Color3.fromRGB(0, 255, 0)
			script.Parent.Sircom.Sound:Play()
			script.Parent.Sircom.Sound.Ended:Wait()
			wait(0.5)
			local tween = ts:Create(gate,gateinfo,{Position = Vector3.new(gatepos.X,gatepos.Y+8,gatepos.Z)})
			tween:Play()
			gate.Sound:Play()
		end
	end
end
1 Like

Try this

local gate = script.Parent.Gate

local ts = game:GetService("TweenService")
local gateinfo = TweenInfo.new(2.12, Enum.EasingStyle.Sine)
local gatepos = gate.Position

local valve1 = script.Parent.Valve1
local valve2 = script.Parent.Valve2
local valve3 = script.Parent.Valve3
local valve4 = script.Parent.Valve4

local code = nil

repeat
	code = (math.random(0, 1) .. math.random(0, 1) .. math.random(0, 1) .. math.random(0, 1))
until code ~= 0000 and code ~= valve1.Value.Value .. valve2.Value.Value .. valve3.Value.Value .. valve4.Value.Value

print(code)

local solved = false

while wait(0.1) do
	if solved == false then
		local plrcode = valve1.Value.Value .. valve2.Value.Value .. valve3.Value.Value .. valve4.Value.Value
		print(plrcode)
		if plrcode == code then
			solved = true
			valve1.Valve.Attachment.ProximityPrompt.Enabled = false
			valve2.Valve.Attachment.ProximityPrompt.Enabled = false
			valve3.Valve.Attachment.ProximityPrompt.Enabled = false
			valve4.Valve.Attachment.ProximityPrompt.Enabled = false
			script.Parent.Light.Color = Color3.fromRGB(0, 255, 0)
			script.Parent.Light.PointLight.Color = Color3.fromRGB(0, 255, 0)
			script.Parent.Sircom.Sound:Play()
			script.Parent.Sircom.Sound.Ended:Wait()
			wait(0.5)
			local tween = ts:Create(gate, gateinfo, { Position = Vector3.new(gatepos.X, gatepos.Y + 8, gatepos.Z) })
			tween:Play()
			gate.Sound:Play()
		end
	end
end
1 Like

You should maybe set this variable after you have completed the door logic and the tween playing on it.

Your wait signal on the whole function is less than the time required to solve and animate the door. Yet you set the ‘solved’ variable as true straight away and expect the function not to re-enter.

That seemed to work I’m running and stopping the game over and over again, and i have not seen the code been 0000 yet! I’ll mark it as a solution after some more testing.

No that wouldn’t be smart. Because then the door opening function is gonna keep repeating over and over again when the code is solved. Until one iteration of the door opening is done.

Yeah a bit of a misnomer there, you never set solved back to false

Well the door has to stay open forever, so setting it back to false will make it repeat again.

Yeah I can see the logic, it is not the way I would do it. This function will run FOREVER, whether the door is open or not, unless you destroy the script manually. There are better methods than an endless while loop.

1 Like

This worked! The door was opening instantly much more before you posted!

I’ll add a break in the function so it stops the loop, it would be stupid to keep looping and doing nothing, it would lag the game.

Absolutely. In fact, there is no need for a loop at all. Just send a message when a code is entered, check it, then animate or not, and then reset…

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