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

How to locate shortened names?

Asked by
n1bu 0
4 years ago

I looked everywhere and I can't find out how to reference shortened names, I want to make a ban GUI and to not have to type the full name, can someone give me a source or a page I can look at how to make this?

1 answer

Log in to vote
0
Answered by
Elixcore 1337 Moderation Voter
4 years ago
Edited 4 years ago
local String = "Alex123"
local Input = "ale"

local Check = string.sub(String, 1, #Input)

--> returns "Ale"

local Lower = string.lower(Check)

--> returns "ale"

if Input == Lower then
   print('true')
end

--> returns "true"

Explanation:

So first we have the player name which we assigned to a variable called "String" The second variable is named "Input" and it's what the player typed in order to find the player.

The first step is using string.sub. This method allows us to transform the string, starting from the second variable and ending in the third. In this case we start on the first letter, and end on the third letter. (#Inputs = 3, as # counts the number of characters in a string, or a number of elements in a table.).

after that we are left with the string "Ale", which is close to "ale", but still has capital letters, so we use string.lower which transform all capital letters from a string into normal ones.

so now we are left with "ale" = "ale".

To check out of a list of players which one matches the name, I would do the following:

local playerTable = {"Eric", "AnDrEw", "Hewl10sj2", "x", "Alex123"}
local Input = "ale"
local player

for i,v in next, playerTable do
   if string.lower(string.sub(v, 1, #Input)) == Input then
      player = v
      break
   end
end

print(player)

What this does is loop through all the players in the table, and the first person with a matching name gets chosen and stops the loop, followed by the printing of the player, in this case "Alex123".

If you were to use this with :GetPlayers() or :GetChildren(), instead of using v, you would need to use v.Name.

hopefully this answers your question.

look more into "string manipulation" in lua, specifically string.sub. And also this is only one way to do it, there are others.

0
thanks n1bu 0 — 4y
Ad

Answer this question