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

How to find players name from part of their name?

Asked by 5 years ago

I have attempted this using string.match, string.gmatch and string.find. The believe the best is string.find.

I want the script to print the players full name from part of their name. As a test I am using my name for an example. This is the script I have.

print(player.Name:find("Craz", 1, true))

This prints 1 4... How can I get this to print the full name?

0
the reason it prints that is that the find function returns an interval in which the pattern was found theking48989987 2147 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

GetPlayers Table


Doing this is pretty simple, you just need to iterate through all the players in a game, then see which one matches the piece given, then break the for loop when it gets to that name, for example:

local matching 
for _,plr in pairs(game.Players:GetPlayers()) do
    if plr.Name:find("Craz") then
        matching = plr.Name
        break
    end
end

print(matching)

Alternatively, if you want to do this inside the loop, you can do the following:

for _,plr in pairs(game.Players:GetPlayers()) do
    if plr.Name:find("Craz") then
        print(plr.Name)
        break
    end
end

please accept if this helped!

0
Is there a way to find text aswell from like a table? So if the message was to say Hi CrazyCorrs could I find HI and only HI? CrazyCorrs 98 — 5y
0
Yes, you can do print(String:sub(String:find("HI"))) Ziffixture 6913 — 5y
Ad

Answer this question