Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to find players name from part of their name?

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?

1 comment

0

the reason it prints that is that the find function returns an interval in which the pattern was found

1 answer

✓ Accepted answer

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!

2 comments

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?

0

Yes, you can do print(String:sub(String:find("HI")))

This discussion is preserved as it was. New answers, comments, and votes are closed.