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

How do I use string.sub to delineate a string?

Asked by 10 years ago

Well, first of all, I know this kind of .Chatted

function test(msg)
Player = script.Parent.Parent
--this script was put into StarterPack.
if msg == "kill/me" then
Player.Character.Humanoid.Health = 0
end
end
script.Parent.Parent.Chatted:connect(test)

Well, I would like to learn about msg:sub(). How does it work? I know that it chops up the string, or something, but I would like to know things like kill/Greatgreatgood5. Can you help?

2 answers

Log in to vote
1
Answered by
Unclear 1776 Moderation Voter
10 years ago

sub chops up the string into two parts, given by its parameters. The first parameter decides where it begins the section, and the second parameter decides where it ends the section. For example...

example = "test"
print(example:sub(1, 2)) -- te
print(example:sub(2, 4)) -- est
print(example:sub(2)) -- est

Notice in the last example that when you leave out the second parameter, it just gives you the rest of the string!

Now, you can use subfor what you're aiming for. However, I personally would use findinstead. Why? Because find can tell me where a certain string begins and ends. In this case, I want to know where the string "/" ends because I know after that string there will be a player name I can work with! So here's an example...

example = "kill/Greatgreatgood5"
print(example:find("/")) -- 5    5

Notice that findreturns two things; the index of where "/" began and where "/" ended. Now that we know the index of where "/" ended, we can try to get the rest of the string using sub!

Here's how that would look...

example = "kill/Greatgreatgood5"
start, finish = example:find("/")
print(example:sub(finish + 1)) -- Greatgreatgood5

Notice that I used finish + 1 as the only parameter in sub; this is because we want to chop off the "/" that we found using find! Now, using the string we get from example:sub(finish + 1), we can find the player we were looking for by looping through all of the players.

example = "kill/Greatgreatgood5"
start, finish = example:find("/")
playerName = example:sub(finish + 1))

for _, player in pairs(game.Players:GetPlayers()) do
    if player.Name == playerName then
        print("Found!")
        -- Do whatever you want to do with the player here
        break
    end
end

Extension What if we had only a chunk of the player's name? How would we find that player? Well, we'd just employ find once more and check whether or not it returns nil; if find cannot find the pattern its given in its parameter, it will return nil!

example = "kill/greatgood"
start, finish = example:find("/")
playerName = example:sub(finish + 1))

for _, player in pairs(game.Players:GetPlayers()) do
    if player.Name:lower():find(playerName:lower()) ~= nil then -- Found a chunk?
        print("Found!")
        -- Do whatever you want to do with the player here
        break
    end
end
Ad
Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
10 years ago

String Library Tutorial

string.sub(a[,b]) returns the characters in a string from a to b (inclusive) or from a and on. negative a is the same as string.length - a

In the specific case of writing "commands," this format would be a more practical method.

Answer this question