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

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

Asked by 5 years ago
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

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

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

lua 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.

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

ending can be negative aswell. Here are examples;

lua 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:

```lua 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.

0
ok i dont understand this at all, only the prints parts but not the function part Gameplayer365247v2 1055 — 5y
0
Which part should I make clearer? What specifically do you not understand ? User#24403 69 — 5y
0
i dont understand the local function part and how i would fit it into player.Chatted function Gameplayer365247v2 1055 — 5y
Ad

Answer this question