Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to successfully mask a text box with "*" for every character entered?

So I am creating a password box for an early-access game I am working on and just for fun, I decided to want to replace every character with "*" whenever entered. I have a simple script below that only changes the first character with the mask but won't allow any other characters to be entered.

Local Script

local box = script.Parent.Password

box:GetPropertyChangedSignal("Text"):Connect(function()
	local strg = box.Text
	
	if string.gsub(strg, "^%.", {}) then
		local new
		
		for i = 1, strg:len() do
			new = "*"
		end
		
		box.Text = new
	end
end)

1 answer

✓ Accepted answer

Hello!

I remember when I attempted to do the exact same thing, when people would enter a password to continue, and then hide their input (or mask it) by having "*" instead of the characters the users had entered.

Now, I thought of the same solution as you, by changing the actual TextBox, however, it just turned up to be buggy, and after a while of thinking I got a solution where I simply had a TextLabel above the TextBox by having the TextLabel's ZIndex higher than the TextBox.

Simply:

  • create TextLabel and have exact same size and position (if you want to..)
  • set the TextLabel's ZIndex to one higher than the TextBox, then you won't be able to see the characters in the TextBox

Then, create a script that edits the TextLabel's text based on amount of characters in the TextBox.

A simple variant:

-- The TextBox
local PasswordInput = script.Parent.PasswordInput;
-- The TextLabel
local PasswordMask = script.Parent.PasswordMask;


PasswordInput:GetPropertyChangedSignal("Text"):Connect( function ()
	local currentPasswordInput = PasswordInput.Text;
	-- Add * based on the amount of characters in the password input
	PasswordMask.Text = ("*"):rep(#currentPasswordInput);
	-- You could alternatively use string.rep("*", #currentPasswordInput)
end)

That's a simple hacky solution, which in my opinion is better than changing the actual TextBox.

I hope this helped, if you have any questions, please ask!

5 comments

1

ok why use semicolons in a language totally irrelevant to JavaScript

0

other languages besides javascript use it aswell :p

1

ok, but this is pointless because semicolons in Lua are used for writing code on the same line not multiple lines

0

Some people do it out of habit from languages such as say C++, Lua is very flexible and can work with semicolons.

2

Semicolons are not just useless in Lua, there are some usecases where you actually need semicolons, such as using anonymous functions. There is nothing wrong with using semicolons, and I originally ignored your post due to this, I thought you knew this but apparently not.

This discussion is preserved as it was. New answers, comments, and votes are closed.