Script doesn't work as intended to

Script

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
image

how can I fix this?

2 Likes
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.

1 Like

also can u please stop messing with scripts like changing the original variables?

I’ve edited the script above since you may only want the auto-complete to work once 3 or more characters have been entered.

https://gyazo.com/b6fcce9041bd73856f80e025cbd03c25

1 Like

Still doesnt work

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.

if string.len(TextBox.Text) < 4 then AutoFillLabel.Text = "" return end

its not what I want too. I want it to see after every space

Not sure what you mean by this (you want spaces to be ignored?).

Lets say

input = "Hey @kidsteve923" -- It wont work
input = "@kidsteve923 hi!" -- wont work
input = "@kidseve923" -- works
1 Like

It only works if I type the name of only 1 player with just 1 word I want it to work with more players and more words.

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)
1 Like

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

one problem it works good but just 1 problem… text wont appear before it : p and spaces still dont work…

Working on my end.
https://gyazo.com/14884761b36ef91575b63f732cc1616b

May need a few tweaks for it to be compatible with your system.

I am using exactly the same script just changing the directories nothing else!

Ahh got it so your system is using another label mine is a label inside a label like a hologram that displays it. like

Text on Text autofill is behind the textbox you see? so thats why for spacing I want it to add all the chars so it spaces correctly.

1 Like

Here’s a basic working example of autocomplete.

We have a collection of potatoes inside the workspace inside a folder.
image

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

Try and implement something similar.

2 Likes

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)

I will test both ideas of @Forummer and @Mister_BlueHat after i uave turned on my pc. Thank u for ur answers :slight_smile:

your system causes the same error. as mine did

can you stop using the space? and use umm like words cuz with spaces it kinda doesnt positions where I want it to…

My script works with multiple objects and multiple words. Was that not the problem?