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))
end end
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.
local function GetPlayerFromString(nameString) local matches= {} for _, player in ipairs (game.Players:GetPlayers()) do end end
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.
if string.lower(player.Name):match(string.lower(nameString)) then table.insert(matches, player) end
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.
if #matches== 1 then return matches[1] else return nil end
Full Function
local function GetPlayerFromString(nameString) local matches= {} for _, player in ipairs (game.Players:GetPlayers()) do if string.lower(player.Name):match(string.lower(nameString)) then table.insert(matches, player) end end if #matches== 1 then return matches[1] else return nil end end
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.
I somehow thought up the code, thank you for the help.
if Cmd:sub(1,6):lower() == (":rank "):lower() then local CheckAfter = Cmd:sub(7,#Cmd) local Player = Players:GetChildren() for i= 1, #Player do if Player[i].Name:lower():sub(1,#Cmd-6) == Cmd:sub(7,#Cmd) then local Rank = Player[i]:GetRoleInGroup(964060) local Num = Player[i]:GetRankInGroup(964060) print(Player[i].Name.." is a "..Rank) local Message = P:WaitForChild("PlayerGui"):WaitForChild("MessageBoard"):WaitForChild("Frame") Message:TweenPosition(UDim2.new(0,0,0,0)) if Num > 0 then Message:WaitForChild("Text").Text = (Player[i].Name.. " is a "..Rank.." of Spectrum Security") elseif Num <= 0 then Message:WaitForChild("Text").Text = (Player[i].Name.." is not in Spectrum Security") end wait(2) Message:TweenPosition(UDim2.new(0,0,-.25,0)) end end end