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

Can anyone explain to me what :sub() and :len() does ?

so like here just a script can anyone explain to me what the :sub() part does. This is just a map changing script. I just don't understand what it does. same with :len()

-- Store all your maps in Lighting.

current_map = nil

names = {}

game.Players.PlayerAdded:connect(function(player)
	for i, v in pairs(names) do
		if v == player.Name then 
			player.Chatted:connect(function(chatmsg)
				if chatmsg:sub(1,4) == "map/" then
					local map = chatmsg:sub(5, chatmsg:len())
					if game.Lighting:FindFirstChild(map) then
						if current_map ~= nil then
							current_map.Parent = game.Lighting
						end
						game.Lighting[map].Parent = game.Workspace
						current_map = game.Workspace[map]
					end
				end
			end)
		end
	end
end)

2 comments

1

len is how long it is and sub is the character in binary between two points

0

instead of len you can also use #StringVariable or #("String")

1 answer

✓ Accepted answer

sub() parses a subsection of the provided string.

local foo = "Abcdef"
print(foo:sub(1,4)) --> Abcd

len() returns the length of the provided string.

local foo = "Abcdef"
print(foo:len()) --> 6

What it seems to do in the instance you have provided above is parse the string from the fifth character to the last character (the message's length). len() actually doesn't need to be used here at all because the second parameter of sub() is optional, and when not provided will automatically parse from the first argument to the end of the string.

1 comment

0

ty

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