Textbox Requires "@" Symbol

I wanted to achieve requiring players to have an “@” symbol at the start of the text inside of text box for more clarification here is an example:

Inside of a text users are required to put there username inside of it but, at the first of the name it needed something like “@” (ex: @UserAsync) if the symbol is not entered it will show an error messege

You can check if the first character of the string is the symbol:

local textbox — your textbox
if string.sub(textbox.Text, 1, 1) ~= “@“ then — get first letter of textbox’s text
— error message
end
1 Like
local ourstring = "Hello @itzmevonkarl1234!"
if not string.find(ourstring,"@") then
     print("Nope")
end

Or

local ourstring = "@itzmevonkarl1234, @Crypt1lc"
local split = ourstring:split(" ")
for i,v in pairs(split) do
     if not v:sub(1,1) == "@" then
          print("Nope")
     end
end

Explaination:

First code sees if there is any @ in a string. We use string.find to do that and then using an if statement we compute our response

Second code sees if there is a @ symbol in each word at the start. Using split we split all the words then using string.sub we get the first letter of said string.

From what I understand this should work:

local Players = game:GetService("Players")

local textBox = script.Parent

local player = Players.LocalPlayer

local requiredText = "@"..player.Name -- You can replace this with "@"..player.DisplayName too if you prefer

local function onFocusLost(enterPressed)
	if enterPressed then
		if string.find(textBox.Text, requiredText) then
			print("Success!")
		else
			warn("Fail!")
		end
	end
end

textBox.FocusLost:Connect(onFocusLost)

Although if the username must always be at the very start of the text, you’ll need to do this instead:

local Players = game:GetService("Players")

local textBox = script.Parent

local player = Players.LocalPlayer

local requiredText = "@"..player.Name -- You can replace this with "@"..player.DisplayName too if you prefer

local function onFocusLost(enterPressed)
	if enterPressed then
		local index = string.find(textBox.Text, requiredText)

		if index and index == 1 then
			print("Success!")
		else
			warn("Fail!")
		end
	end
end

textBox.FocusLost:Connect(onFocusLost)

To be honest though, an example would be very helpful for us to understand what you’d like the end result to look like

you can use a string pattern which is the following: “^@”
(^ is an anchor which restricts it to only be the first character)

local match = string.match(text, '^@')

if not match then
   -- doesnt have @ (AT THE START)
end

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