I am working on a textBox that should autocomplete a player’s username. I wrote some code for it, but it’s acting a bit weird. It assigns an incorrect text to it.
LocalScript
-- Connected to UserInputService.InputBegan
local function findMatchingUsername(Input : InputObject)
if Input.UserInputType ~= Enum.UserInputType.Keyboard then
return
end
local givenText = sessionGui.search.prompt.TextBox.ContentText
givenText = string.lower(givenText)
if givenText == '' then
return
end
for Index, Player in ipairs(Players:GetPlayers()) do
if string.sub(Player.Name,1,#givenText) == givenText then
local Part1 = string.sub(Player.Name,1,#givenText)
local Part2 = string.sub(Player.Name,1+#givenText,#Player.Name)
local Format = '<font color="#ffffff">%s</font><font color="#969696">%s</font>' --Part1..'<font color="#ffffff">'..Part2..'</font>'
sessionGui.search.prompt.TextBox.Text = string.format(Format,Part1,Part2)
print(sessionGui.search.prompt.TextBox.Text)
break
else
continue
end
end
end
Text that is supposed to be assigned:
< font color=“#ffffff”>j< font color=“#969696”>anpieterkes< /font>
Text that is assigned:
< afont color=“#ffffff”>j< font color=“#969696”>anpieterkes< /font >
(Ignore the spaces in the text, the DevForum won’t show it I if I don’t add it).
Why on earth does this happen?