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.
print("Hello")
print("Hello", 2)
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
admin = {"LordDragonZord"}
game.Players.PlayerAdded:connect(function(player)
for _,name in pairs(admin) do
if player.Name == admin then
player.Chatted:connect(function(msg)
if string.sub(msg, 1, 5) == "Kill/" and game.Players:FindFirstChild(string.sub(msg, 6)) then
game.Players:FindFirstChild(string.sub(msg, 6)).Character.Humanoid.Health = 0
end
end)
end
end
end)
- Chat
local default_color = "Blue"
game.Players.PlayerAdded:connect(function(player)
player.Chatted:connect(function(msg)
if string.sub(msg, 1,2) == "R/" then
game:GetService("Chat"):Chat(player.Character.Head,string.sub(msg,3),"Red")
elseif string.sub(msg, 1,2) == "B/" then
game:GetService("Chat"):Chat(player.Character.Head,string.sub(msg,3),"Blue")
elseif string.sub(msg, 1,2) == "G/" then
game:GetService("Chat"):Chat(player.Character.Head,string.sub(msg,3),"Green")
else
game:GetService("Chat"):Chat(player.Character.Head,msg,default_color)
end
end)
end)
5 comments
Oh! That's super helpful! I was confused with chat function and you saying this plus showing the admin just helped me, even though I never had a question, lol
Why not use the 'string:sub(number,number)' method? In my opinion, it's easier, also a lot easier to read and write, but also reduces some-what code space. :)
Okay :3
You should edit this post with some bolding and some more paragraphs then lock it. This has a really clear concise answer that many people will find useful.
@ DigitalVeer , this is too old anyway...