string.sub, sub means substring or part-of-a-string. It's basically taking a string and cutting off a piece of it. e.g.
In string.sub it looks like something like this:
string.sub("String", 2,4)
. string.sub is you know, string.sub. In the parentheses you have your string. "Hi", "Text", "string". Next you have a number. If it's just one number, that means anything before this number will be cut off. string. string.sub("String", 2) == "tring" --t is the 2nd number, 1 is before it so it gets cut off.
If there is 2 numbers(you can only have 1 to 2 numbers) then it will cut off anything before the 1st number and anything past the 2nd number. string.sub("String", 2,4) == "tri" --ng and S get cut off.
If you're wondering how do we know what numbers to use, lets say we have this weird string that has a message. "afakwshellosadffgafsadsadfge" we want it to say "hello". string.sub("afakwshellosadffgafsadsadfge", 6,11) --the 6th character in the string is "s". Everything before it(afakw) also get's deleted from the string. Now the 11th character is "s" again coincidentally. "s" and everything AFTER s is deleted(adffgafsadsadfge).
Hope it helps! If you're still having trouble understanding, please tell me!
Examples of string.sub in scripts:
- Admin Scripts
- Chat service
- Admin
01 | admin = { "LordDragonZord" } |
03 | game.Players.PlayerAdded:connect( function (player) |
05 | for _,name in pairs (admin) do |
06 | if player.Name = = admin then |
07 | player.Chatted:connect( function (msg) |
08 | if string.sub(msg, 1 , 5 ) = = "Kill/" and game.Players:FindFirstChild(string.sub(msg, 6 )) then |
09 | game.Players:FindFirstChild(string.sub(msg, 6 )).Character.Humanoid.Health = 0 |
- Chat
01 | local default_color = "Blue" |
03 | game.Players.PlayerAdded:connect( function (player) |
04 | player.Chatted:connect( function (msg) |
05 | if string.sub(msg, 1 , 2 ) = = "R/" then |
06 | game:GetService( "Chat" ):Chat(player.Character.Head,string.sub(msg, 3 ), "Red" ) |
07 | elseif string.sub(msg, 1 , 2 ) = = "B/" then |
08 | game:GetService( "Chat" ):Chat(player.Character.Head,string.sub(msg, 3 ), "Blue" ) |
09 | elseif string.sub(msg, 1 , 2 ) = = "G/" then |
10 | game:GetService( "Chat" ):Chat(player.Character.Head,string.sub(msg, 3 ), "Green" ) |
12 | game:GetService( "Chat" ):Chat(player.Character.Head,msg,default_color) |
Locked by Zafirua, User#5423, and theCJarmy7
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?