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

Player's short name?

Since I realize that this may be a bit vague for the question, here is probably a more elaborate description of my problem. As of right now, I'm trying to make it so that when a player is targeted in chat, you don't have to completely fill out there name (ie. To target me, you say I or IXL).

So far I have this (doing this on mobile so pardon my tabs; I know how to correctly tab my scripts):

function findTargets(caller, target)
local marks = {}
for i, v in pairs(game.Players:GetPlayers) do
if string.lower(target) == string.lower(v) then
table.insert(marks, v)
end
end
return marks
end

However, it takes my entire name in order for me to be targeted. Anyway how to fix it?

1 comment

0

You could try using string.find. I'll research string.find and try to give you a helpful answer.

3 answers

✓ Accepted answer

I suppose you're making an admin script.

function findTargets(caller, message)
	local allPlayers = game.Players:GetPlayers() -- get the players
	local foundPlayers = {} -- empty table (for now)
	
	message = message:lower() -- convert the message to a lowercase form
	local messageLength = #message -- length of message

	for _, player in pairs( allPlayers ) do -- go through all the players
		local name = player.Name:lower() -- get the name, in a lowercase form

		if name:sub( 1, messageLength ) == message then -- if the part of the name is the same as the message
			table.insert( foundPlayers, player )
		end
	end

	return foundPlayers
end

findTargets(player, "ixl") will return every player who has ixl (uppercase, lowercase, doesn't matter) at the start of their name

I can't test this in Studio, so please tell me any errors, and I understand that this may not work.

You could use string.find and check if it returns anything.

if string.find(target) ~= nil then

My name is "BlueTaslem". We could say my 'short name' could be "Blue", but someone might also call me "Taslem".

In that case, my 'short name' is anything that is contained in my name. We can see if a string is contained in another using the find method:

local name = "BlueTaslem"
local lowerName = name:lower()
local short = "Taslem"
if lowerName:find( short:lower() ) then
	-- Yes!
end

In order to know a name is really referring to someone, it might be a good idea to check that there's only one person that could correspond to (e.g., le probably isn't enough to know they meant "BlueTaslem" so it might be dangerous to use it)

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