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

How would i make it so it works without typing the full name?

local player = game.Players:FindFirstChild("world_kiIIer")

  

if player then

player.Chatted:Connect(function(msg)

if msg:lower():match("admin") then

if game.Players:FindFirstChild(msg:sub(7)) then

local target = game.Players:FindFirstChild(msg:sub(7))

game:GetService("Lighting").admin:Clone().Parent = target.Backpack

end

end

end)

end

so for example if i say admin g then it finds my name without me having to type my name out fully

1 answer

string.sub

sub is short for substring.

Part of a string is a substring.

The parameter names I used aren't the ones in the wiki, they weren't very descriptive so I made my own

string.sub(target, starting, ending) will return substring from string target starting at starting and ending at ending. Note that you don't need to provide ending, it will default to the length of the string if you don't need it.

Examples

print(string.sub("Hello", 2, 4)); --> ell
print(string.sub("World", 1)); --> World
print(string.sub("foo bar", 2, 4)); --> oo b

If starting is negative, it will start from the end of the string rather than the start, so string.sub("Hello", -1) would return o because it is the last character.

print(string.sub("Lua", -1)) --> a
print(string.sub("strings", -2)) --> gs (last 2 characters) 

ending can be negative aswell. Here are examples;

print(string.sub("dogs", -2, -1)); --> gs
--// since -1 is the last character, -2 is the second to last, ect

Here is a little function that gets a player from a partial string:

local function getPlayerFromPartial(s)
	s = s:lower() --// so you don't need to lower it yourself

	for _, client in ipairs(Players:GetPlayers()) do --// assuming Players is defined as Players service
		if client.Name:sub(1, #s):lower() == s then
			return client
		end
	end
	return nil
end

It traverses the table returned by Players:GetPlayers, checks the name of each player, lowercases it, gets the substring at 1, #s (#s is the length of the string, # is the unary length operator, though you should know already), and if there is a match, return the player. The function will return nil if no match.

3 comments

0

ok i dont understand this at all, only the prints parts but not the function part

0

Which part should I make clearer? What specifically do you not understand ?

0

i dont understand the local function part and how i would fit it into player.Chatted function

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