Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is string.sub and how do you use it?

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

1 answer

✓ Accepted answer

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:

  1. Admin Scripts
  2. Chat service

  1. Admin
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)
  1. 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

0

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

0

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. :)

0

Okay :3

0

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.

0

@ DigitalVeer , this is too old anyway...

This discussion is preserved as it was. New answers, comments, and votes are closed.