Hi, thank you for reading. After doing some digging and trying to understand it from wiki, I'm still confused. Why? On Wiki.Roblox, it said:
Returns the substring of s that starts at i and continues until j; i and j may be negative. If j is absent, then it is assumed to be equal to the length of s. In particular, the call string.sub(s,1,j) returns s until as many characters as j, and string.sub(s, -i) returns a suffix of s with length i.
This is a really weird description and if anyone can give me a simpler description, I would be really grateful.
~Thank you for reading, Yeevivor4
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") --Hello print("Hello", 2) --Ello
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 = {"LordDragonZord"} game.Players.PlayerAdded:connect(function(player) -- player.CharacterAdded:connect(function(char) --This isn't really necessary. 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) end)
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)
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?