So, say, I were to look for my username
if game.Players:FindFirstChild("Wee")
print(true)
end
Would this work, and if not, does Roblox’s string.find or string.match match string from only the first character?
So, say, I were to look for my username
if game.Players:FindFirstChild("Wee")
print(true)
end
Would this work, and if not, does Roblox’s string.find or string.match match string from only the first character?
Whats the reason you are looking for a player just using some characters from player’s name?
Maybe creating a table/dictionary that matches the player instance and the “nickname” Wee = Weeabus
?
Wouldnt be better just provide the whole name? or instances from events? instead of spliting the name and trying to find that string?
I’m doing some commands. Some people may be lazy and only type part of the names. Plus, some people have usernames that looks like someone just smashed their head on the keyboard.
You got any more efficient ways?
Probably there’s a way, to find a name by the first characters, but it will have errors if theres another guy called Weeboo and you Weeabus.
Wouldnt be better, create a console gui, with buttons then? Or maybe assign a nickname, stored in a table, and the player just use the chat command with that nickname, maybe.
xDDDD. True!
You could use string.sub to match whether String A and String B have a partial part of something. Let’s analyze the example below:
local stringA = "Ro"
-- stringB = Player's partial name containing specific characters from stringA
for i,plr in next, game:GetService'Players':GetPlayers() do
if plr.Name:sub(1,#stringA) == stringA then
print(plr.Name.." has similar phrase with stringA")
end
end
stringB is the player’s Name. The for
loop detects whether the player’s Name matches the partial string. For example, if the Roblox search bar input is Adopt
, and the code above detects whether Adopt Me
is a partial string. If it is, the code would print Adopt Me has a similar phrase with Adopt
.
I am proud to say that anything that contains “Wee” as the first three letter and looks any percent like “Weeaboo” will not be created anymore, as Roblox actually flagged it in the character creation, meaning my username is somewhat illegal.
xD okie. Then, use that method! If you are perfectly sure that will never exist another player named like you and you are not using this system with other players, then you will never have errors with this method. xD Illegal named guy xD
Unfortunately, it wouldn’t be possible for FindFirstChild
function, but you could probably made a custom function like below:
local function FindFirstChild(parent, objname)
for i,o in next, parent:GetChildren() do
if o.Name:sub(1,#objName) == objName then
return o
end
end
end
This can be done simpler and more precise, the example posted is restricted as it is case sensitive and doesn’t utilize string patterns which are used with string.match
, the following function works the same way as if you were to use FindFirstChild, but matching the start of your input.
local function GetPlayerFromShort(shortName)
shortName = string.lower(shortName)
for _,Player in pairs(game.Players:GetPlayers()) do
local playerName = string.lower(Player.Name)
if string.match(playerName, "^"..shortName) then
return Player
end
end
end
GetPlayerFromShort("pro")
-- > Proxus [Player]
You could also try using string.find
, if you have cases where you would search abus
instead of Wee
.
And then you can for loop the children and find the player first with abus
in their name:
local function getPlayerFromPartialName(partialName)
partialName = partialName:lower()
for _,player in ipairs(game:GetService("Players"):GetPlayers()) do
if string.find(player.Name:lower(),partialName) then
return player
end
end
end