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

Locating shortened names?

Asked by 9 years ago

I am trying to make an admin script that ranks players when the admin says ":rank " and then any player's name.

I've accomplished this, but you need to say the whole name in it's entirety which can be a lot of unnecessary typing.

What I wish to do is for the admin to type a shortened version of the player's name. (Ex: "Shedletsky", :rank Sh)

I've tried many methods from others that have helped, but all have led to me becoming more confused and not learning how to do this, which is the ultimate goal. What would you suggest?

for i,v in pairs (game.Players:GetChildren()) do if Cmd:lower() == (":rank "..v.Name):lower() then local Message = P:WaitForChild("PlayerGui"):WaitForChild("MessageBoard"):WaitForChild("Frame") Message:TweenPosition(UDim2.new(0,0,0,0)) if v:GetRankInGroup(964060) > 0 then Message:WaitForChild("Text").Text = (v.Name.." is a "..v:GetRoleInGroup(964060).." of Spectrum Security") elseif v:GetRankInGroup(964060) <= 0 then Message:WaitForChild("Text").Text = (v.Name.." is not in Spectrum Security") end wait(2) Message:TweenPosition(UDim2.new(0,0,-.25,0))

1    end
2end
0
Put the code inside the two long "~~~~" Lines. xPolarium 1388 — 9y
0
^_^ Discern 1007 — 9y

3 answers

Log in to vote
3
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

This is a case where String patterns are very powerful. For reference check out this wiki page on string patterns.

Overview

The goal is to find the player whom's name starts with a couple of characters the user has entered. To do this, we'll need to use a function that searches through all the players in the game and stores any matches in a variable. It will be good to store the matches in a table in case there are more than one.

1local function GetPlayerFromString(nameString)
2    local matches= {}
3    for _, player in ipairs (game.Players:GetPlayers()) do
4 
5    end
6end

Here is where string patterns come in to play. Using the function of strings called match, we can match characters to portions of the string. You may want to do some research on what quantifiers, classes, or anchors you want to use. In this case we want to use the "^" anchor to make it match only the start of the string. It might be good to convert all the characters to lowercase or uppercase so that it isn't case sensitive.

1if string.lower(player.Name):match(string.lower(nameString)) then
2    table.insert(matches, player)
3end

Sense you are making admin commands, I assume you want to only have one match. Using and if loop we can keep the data if there is more than one match and throw it away if there are multiple matches.

1if #matches== 1 then
2    return matches[1]
3else
4    return nil
5end

Full Function

01local function GetPlayerFromString(nameString)
02    local matches= {}
03    for _, player in ipairs (game.Players:GetPlayers()) do
04        if string.lower(player.Name):match(string.lower(nameString)) then
05            table.insert(matches, player)
06        end
07    end
08    if #matches== 1 then
09        return matches[1]
10    else
11        return nil
12    end
13end
0
Interesting! You could improve by adding a "^" to line 4. To make it like this: Tweakified 117 — 5y
Ad
Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
9 years ago

Using ROBLOX Wiki, you can find a list of generic functions to manipulate strings. One of which could help you in this case. Which is string.match. You can try using this to not type the entire "Username" of a player.

More on String.match and String Manipulation.

0
I would try implementing this on your code but your code is not properly Code Blocked and doesn't look relevant to what you're asking. xPolarium 1388 — 9y
Log in to vote
0
Answered by 9 years ago

I somehow thought up the code, thank you for the help.

01if Cmd:sub(1,6):lower() == (":rank "):lower() then
02                    local CheckAfter = Cmd:sub(7,#Cmd)
03                    local Player = Players:GetChildren()
04                    for i= 1, #Player do
05                        if Player[i].Name:lower():sub(1,#Cmd-6) == Cmd:sub(7,#Cmd) then
06                            local Rank = Player[i]:GetRoleInGroup(964060)
07                            local Num = Player[i]:GetRankInGroup(964060)
08                        print(Player[i].Name.." is a "..Rank)
09                        local Message = P:WaitForChild("PlayerGui"):WaitForChild("MessageBoard"):WaitForChild("Frame")
10                        Message:TweenPosition(UDim2.new(0,0,0,0))
11                            if Num > 0 then
12                                Message:WaitForChild("Text").Text = (Player[i].Name.. " is a "..Rank.." of Spectrum Security")
13                            elseif Num <= 0 then
14                                Message:WaitForChild("Text").Text = (Player[i].Name.." is not in Spectrum Security")
15                            end
16                        wait(2)
17                        Message:TweenPosition(UDim2.new(0,0,-.25,0))
18                        end
19                    end
20                end

Answer this question