local UIS = game:GetService("UserInputService")
local box = script.Parent
local autofill = script.Parent.Autofill
local autofillafter_chars = require(game.ReplicatedStorage.Config).FillAfterTypingMaxChars
function FindPlayer(name)
name = name:gsub("@","")
for i,v in pairs(game.Players:GetPlayers()) do
if v.Name:lower():sub(1,#name) == name:lower() then
return v
end
end
end
box:GetPropertyChangedSignal("Text"):Connect(function()
local space = box.Text:split(" ")
local name = space[#space]
if name:sub(1,1) == "@" and name ~= "" and name:len() > autofillafter_chars and FindPlayer(name) then
autofill.Text = box.Text:gsub(name,"@"..FindPlayer(name).Name) -- possible line of error.
UIS.InputBegan:Connect(function(i)
if i.KeyCode == Enum.KeyCode.Tab then
box.Text = autofill.Text
end
end)
else
autofill.Text = ""
end
end)
Issue
Ok so I am making a player autofill system like when player types like @kid it would autofill it to @kidsteve923 it does do that but if its like @kidsteve923 hey @kid it would show this text in the autofill label
local Game = game
local Script = script
local Players = Game:GetService("Players")
local Frame = Script.Parent
local TextBox = Frame.TextBox
local AutoFillLabel = Frame.AutoFillLabel
local function OnTextBoxTextChanged()
if string.len(TextBox.Text) < 4 then return end
local ThePlayer
for _, Player in ipairs(Players:GetPlayers()) do
if not string.match("@"..string.lower(Player.Name), "^"..string.lower(TextBox.Text)) then continue end
ThePlayer = Player
end
AutoFillLabel.Text = if ThePlayer then ThePlayer.Name else ""
end
TextBox:GetPropertyChangedSignal("Text"):Connect(OnTextBoxTextChanged)
Use string.match with the ‘^’ anchor (start of string) to prevent this issue.
local Game = game
local Script = script
local Players = Game:GetService("Players")
local TextBox = script.Parent
local AutoFillLabel = TextBox.Autofill
function FindPlayer(name)
name = name:gsub("@","")
for i,v in pairs(game.Players:GetPlayers()) do
if v.Name:lower():sub(1,#name) == name:lower() then
return v
end
end
end
local function OnTextBoxTextChanged()
if string.len(TextBox.Text) < 4 then return end
local ThePlayer
local name = TextBox.Text:split(" ")[#TextBox.Text:split(" ")]
for _, Player in ipairs(Players:GetPlayers()) do
if not string.match("@"..string.lower(Player.Name), "^"..string.lower(name)) then continue end
ThePlayer = Player
end
local plr = FindPlayer(name)
AutoFillLabel.Text = if plr and ThePlayer then "@"..ThePlayer.Name else ""
end
TextBox:GetPropertyChangedSignal("Text"):Connect(OnTextBoxTextChanged)
its not what I want too. I want it to see after every space also after removing the player name the autofill still stays.
local Game = game
local Script = script
local Players = Game:GetService("Players")
local Frame = Script.Parent
local TextBox = Frame.TextBox
local AutoFillLabel = Frame.AutoFillLabel
local function OnTextBoxTextChanged()
local ThePlayers = {}
for Name in string.gmatch(string.lower(TextBox.Text), "@%S+") do
for _, Player in ipairs(Players:GetPlayers()) do
if not string.match("@"..string.lower(Player.Name), "^"..string.lower(Name)) then continue end
table.insert(ThePlayers, "@"..Player.Name)
end
end
AutoFillLabel.Text = table.concat(ThePlayers, " ")
end
TextBox:GetPropertyChangedSignal("Text"):Connect(OnTextBoxTextChanged)
For every occurrence of the @ character, you need to use the function. Right now you are only checking the first character. Try parsing the whole string, looking for every occurrence of the @ character and then replacing the abbreviated name with the actual name
We have a collection of potatoes inside the workspace inside a folder.
Now we have a script that takes an input string and autocompletes the names of the items
INPUT_STRING = "@Rot . @Pot is delicious @Rot however is not delicious"
My_Items = workspace.My_Items
function findAbbreviation(str)
local SplitString = string.split(str, " ")--split the string up
for _,v in pairs(SplitString) do
if string.sub(v,1,1) == "@" then --if a string starting with @ is found
for _,item in pairs(My_Items:GetChildren()) do
local Cmd = string.gsub(v,"@","") --get the command without the @ symbol
local match_begin = string.find(item.Name,Cmd) -- the index where the match was found
if match_begin == 1 then --Check if the match was found at the beginning
str = string.gsub(str,"@"..Cmd,item.Name) --replace the found string with the item's name
end
end
end
end
return str
end
print(findAbbreviation(INPUT_STRING))
The result is this
>>>> Rotten_Potato_In_Game . Potato_In_Game is delicious Rotten_Potato_In_Game however is not delicious
I see, you want to preserve the number of characters (and replace with spaces) before the ‘mention’ occurs.
local Game = game
local Script = script
local Players = Game:GetService("Players")
local Frame = Script.Parent
local TextBox = Frame.TextBox
local AutoFillLabel = Frame.AutoFillLabel
local function OnTextBoxTextChanged()
local PlayerNames = {}
for Name in string.gmatch(string.lower(TextBox.Text), "@%S+") do
if string.len(Name) < 4 then continue end
local Start, End = string.find(string.lower(TextBox.Text), Name)
for _, Player in ipairs(Players:GetPlayers()) do
if not string.match("@"..string.lower(Player.Name), "^"..string.lower(Name)) then continue end
table.insert(PlayerNames, string.rep(" ", Start - 1).."@"..Player.Name)
end
end
AutoFillLabel.Text = table.concat(PlayerNames, "~")
end
TextBox:GetPropertyChangedSignal("Text"):Connect(OnTextBoxTextChanged)