Password Script using TextButtons only working in Run mode

So I have this “computer” (really just a block with a SurfaceGui) and the point of my script is that if the correct code is entered, the monitor will become slightly transparent. The issue is when I try to enter the code to make the monitor transparent in Play Solo mode, clicking the numbers on the numpad does…nothing. But, when I do it in Run mode, it works perfectly fine.

The following script is from a free model code door that I edited to be used on a ScreenGui.

numpadz = game.Workspace.Monitor.SurfaceGui.Frame
thecode = "4958"
input = ""

--CLEAR FUNCTION
numpadz.Clear.Activated:Connect(function()
	input = ""
	print("Code Cleared")
end)

--ENTER FUNCTION
numpadz.Enter.Activated:Connect(function()
	if input == thecode
	then
		workspace.Monitor.Transparency = .7
	return end
	input = ""
	print("Incorrect Code")
end)

--ENTER #0
numpadz.N0.Activated:Connect(function()
	input = input..0
	print("Enter 0")
	print(input)
end)

--ENTER #1
numpadz.N1.Activated:Connect(function()
	input = input..1
	print("Enter 1")
end)

--ENTER #2
numpadz.N2.Activated:Connect(function()
	input = input..2
	print("Enter 2")
end)

--ENTER #3
numpadz.N3.Activated:Connect(function()
	input = input..3
	print("Enter 3")
end)

--ENTER #4
numpadz.N4.Activated:Connect(function()
	input = input..4
	print("Enter 4")
end)

--ENTER #5
numpadz.N5.Activated:Connect(function()
	input = input..5
	print("Enter 5")
end)

--ENTER #6
numpadz.N6.Activated:Connect(function()
	input = input..6
	print("Enter 6")
end)

--ENTER #7
numpadz.N7.Activated:Connect(function()
	input = input..7
	print("Enter 7")
end)

--ENTER #8
numpadz.N8.Activated:Connect(function()
	input = input..8
	print("Enter 8")
end)

--ENTER #9
numpadz.N9.Activated:Connect(function()
	input = input..9
	print("Enter 9")
end)

Thank you in advance!

Are all of your number buttons (N1, N2, N3, etc) TextButtons? If so, I recommend using MouseButton1Click rather than Activated. I’m not sure if this will fix your problem though.

^ just kidding on that part, Activated is better.

Also, is this running in a LocalScript or a server Script?

Welp, I changed it to MouseButton1Click while you were editing, and it works. I’m not sure how Activated is better, feel free to explain. if you’d like

P.S. All of the buttons are TextButtons and it is in a server Script.

Activated is meant to work cross-platform, while MouseButton1Click (I assume) only works with a mouse&keyboard setup (which is 99% of the time only PC).

I’m not sure why the latter works in this case but the former doesn’t, because as far as I knew, SurfaceGui button presses were replicated to the server. Just in case they aren’t though, it may be worthwhile to put it in a LocalScript instead and have a second server Script in ServerScriptService to catch events. Maybe this way you’d still be able to use Activated?

Here’s my take on it:

local TheCode = "4958"
local Input = ""
local Btns = game.Workspace.Monitor.SurfaceGui.Frame

Btns.Clear.Activated:Connect(function()
	Input = ""
	print("Code Cleared")
end)

--ENTER FUNCTION
Btns.Enter.Activated:Connect(function()
	if Input == TheCode then
		workspace.Monitor.Transparency = .7
	else
		Input == ""
		print("Incorrect Code")
	end
end)

for i,v in pairs(Btns:GetChildren()) do
	if string.sub(v.Name, 1, 1) == "N" then
		v.Activated:Connect(function()
			local Add = string.sub(v.Name, 2, #v.Name)
			Input = Input..Add
		end)
	end
end

When scripting, Instead of writing things out manually, You can make an automatic system.

Also also, I’d like to mention, you used return and honestly that wasn’t necessary, You can just use else as I did above.

Alright, I may try that. Also, it turns out that Roblox is planning on deprecating MouseButton1Click in favor of Activated.

Oh wow, good to know.

Yeah, then your best bet would definitely be a LocalScript that catches the .Activated events and transmits the necessary information via RemoteEvents to a separate script somewhere else (ServerScriptService would be best imo).

Let me know if this works, as I haven’t done any testing.

It’s probably worth trying to make it work with .Activated so it doesn’t need to be revisited or just so you learn the ins and outs.

Mobile is taking the world by storm… a little at a time?
Tiny Giant, just on the horizon so watch out?

Eh, it’s a chance to get ahead of the curve :stuck_out_tongue:

1 Like

I’ve used MouseButton1Click before, and it appears to work perfectly cross platform others say. It could just be a problem within your code. Perhaps it stops functioning somewhere.