Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
1
Answered by 5 years ago

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!

1
ok why use semicolons in a language totally irrelevant to JavaScript DeceptiveCaster 3761 — 5y
0
other languages besides javascript use it aswell :p theking48989987 2147 — 5y
1
ok, but this is pointless because semicolons in Lua are used for writing code on the same line not multiple lines DeceptiveCaster 3761 — 5y
0
Some people do it out of habit from languages such as say C++, Lua is very flexible and can work with semicolons. User#24403 69 — 5y
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. 1TheNoobestNoob 717 — 5y
Ad

Answer this question