Theres a thing that can count characters(string.len()) but theres no thing that can count lines, so how can i count lines in text? i need it for custom chat.
To count the number of lines in a text in Roblox Studio in LuaU, you can use the function that will split the text by newline characters (\n) and count their number. Below is an example of how this can be done:
local function countLines(text)
local count = 0
for _ in string.gmatch(text, "[^\n]*\n?") do
count = count + 1
end
return count
end
– Usage example
local exampleText = “First line\Second line\nth line”
print(countLines(exampleText)) – Outputs: 3
In this example, string. gmatch is used to find substrings that match the regular expression [^\n]*\n?, which means “any number of non-newline characters that can be followed by a newline character”. Then we simply increment the counter for each substring found.
You can also use the string. split function from the community libraries, but this is not a built-in function in LuaU. It looks like:
local function split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
table.insert(t, str)
end
return t
end
local function countLines(text)
local lines = split(text, "\n")
return #lines
end
– Usage example
local exampleText = "First line\Second line\nth line"
print(countLines(exampleText)) -- Outputs: 3
In this case, the split function splits the text into substrings using the \n newline character as the separator, and then the countLines function returns the number of substrings received.
Choose the method that suits you best for your task.
Oh wait thanks, let me check rq
wait that works! thanks you! that was a bit harder than i was thinkink
I am not quite sure why this guy wanted to make his own split function.
local function countLines(text)
local lines = text:split("\n")
return #lines
end
idk lol im not that pro in scripting
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.