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

What would I use to make an auto completion in a string?

Asked by 5 years ago
script.Parent.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
        for _,v in pairs(game.Players:GetPlayers()) do
            if string.match(script.Parent.TextBox.Text, v.Name) then
                print(v)
        end
    end
end)

It only returns v if I write the full name, when I want it to match the first words in a string. How would I fix it so that it auto completes if I start writing a name?

2 answers

Log in to vote
1
Answered by 5 years ago

string.match

The first parameter is the string, the second is the pattern you are looking for. It should be the player's Name first and the TextBox.Text second.

string.match(plr.Name, TextBox.Text)

string.match will match anywhere in the string. string.match('helloPizza', 'Pizza') will return something. Use string.sub to stop this interaction.

string.lower, string.upper

string.match is case sensitive. By making the string lowercase/uppercase, it'll compare just the letters rather than checking if upper/lower letters are matching.

local lowerName = plr.Name:lower()
local lowerText = TextBox.Text:lower()

string.match(lowerName, lowerText)

Matching Nothing

Having the string be nothing "" will match with any player. You should check if the TextBox.Text is not equal to "" before running anything. Since you are autofilling, don't think this matters.

More

Since people could have similar names, you should not autofill until there is only one person that matches the string.

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
    local matchedPlayers = {}

    for _,v in pairs(game.Players:GetPlayers()) do
        if string.match(v.Name:lower(), script.Parent.Text:lower()) then
            table.insert(matchedPlayers, v)
        end
    end

    if (#matchedPlayers == 1) then
        local autofill = matchedPlayers[1]
    end
end)

Good luck!

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
script.Parent.TextBox.Changed:Connect(function()
for i,v in pairs(game.Players:GetChildren()) do
if v.Name:match:(script.Parent.TextBox.Text:lower()) then
print(v)
end
end
end)

I believe this will work, it checks the players name when the text box text is changed and lowercases it. Wrote this on my phone so it might not be fully right (typos)

Answer this question