im trying to get the number of lines and display it. currently it works however it ignores lines that the playered pressed enter on.
this wouldnt be so bad if the thing im trying to put the lines for is a script executor for my admins. (proper formatting will have lots of enters)
function getNumberOfLines(str)
local count = 0
for line in string.gmatch(str, "[^\n]+") do
count = count + 1
end
return count
end
SERVER.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
local lines = getNumberOfLines(SERVER.TextBox.Text)
local str = ""
for i=1, lines do
str = str .. i .. "\n"
end
print(str)
SERVER.TextLabel.Text = ">"..str
end)
the reason is stops short is becuase it doesnt counter enter as a space
note: THIS IS NOT AN EXPLOIT THIS IS A CLIENT SIDE EXECUTOR FOR ME AND DEVS TO RUN SERIES OF COMMANDS IN GAME
function getNumberOfLines(str)
local count = 0
for line in string.gmatch(str, "[^\n]+") do
count = count + 1
end
return count
end
SERVER.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
local lines = getNumberOfLines(SERVER.TextBox.Text)
local str = ""
for i=1, lines do
str = str .. i .. "\n"
end
print(str)
SERVER.TextLabel.Text = ">"..str
end)
This pattern matches one or more newlines, each of which must be at the beginning of a string. Since only one character can ever be at the beginning of a string, only one such newline can possibly be matched. So really it’s the same as “^\n”, i.e. match only the first character, and only if it’s a newline. You want every newline in a string, so don’t use the caret. Just matching on \n is sufficient.
function countMatches(s, pattern)
local count = 0
for match in s:gmatch(pattern) do
count = count + 1
end
return count
end
function countLines(s)
return 1 + countMatches(s, "\n") --Plus 1 because 1 lines has 0 line changes (see "fencepost problem" / off-by-one errors)
end
local s = [[a --1
b --2
c --3
--4
-- 5
]] --6
print(countLines(s)) --6
That pattern matches 1 or more non-newline characters, take note of the carot key at the start of the character set.
local s = [[a --1
b --2
c --3
--4
-- 5
]] --6
local c = 0
for _s in s:gmatch("[^\n]+") do
c += 1
end
print(c) --5
The reason it doesn’t work as intended is because some lines contain nothing but a newline character, the + modifier searches for a pattern which matches one or more occurrences of its preceding character class/character set.