Splitting a Textbox into Lines and then Looping through the Lines

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m programming a game about programming, using a made up language which is written in a text box and ran with a button that splits and checks the code that you write and then runs real functions based on what you wrote, and I need to make it so that each line’s code is counted as seperate code instead of being bunched into one line in the “compiler” so that errors and functions can be ran properly
  2. What is the issue? Include screenshots / videos if possible!
    I’m not able to get it to work, since I don’t know how I can apply the current line being checked to the code “compiler”. Here is the code I have at the moment for looping through the lines. (Full code not shown for security reasons in the future)
-- Other Variables above this in the actual script
local i = 0
local lines = string.split(code, '\n')

	for i = i, #lines do
		i = i + 1
		local args = string.split(lines[i],':')
	if not args[2] == nil then
		newargs = string.gsub(args[2], '"', "")
-- Continues in the actual script

Please help me figure out how to treat each line as its own little section of code in the “compiler”. Sorry if my post was convoluted or made no sense, its 1 am and it’s pretty hard to describe exactly what I am trying to acheive. (The args are the “functions” and their paramaters, I am trying to get the arg splitter to split only what is on the currently checked line)

So u are trying to make code executor in your game?

if so u should totally try Lua VM

Well, kind of, but not really since its using a made up language and not RLua

Ah i see then

local i = 0
local lines = string.split(code, '\n')

for _,line do (lines) do
	local args = string.split(line,':')
    if not args[2] == nil then
	   newargs = string.gsub(line, '"', "")
    end
end
1 Like
for line in string.gmatch(code, "^.*$") do
	local args = string.split(line, ":")
	if not args[2] then
		newargs = string.gsub(line, "\"", "")
	end
end

Thank you so much! I’ll try it out and I’ll mark your answer as the solution if it works.

It doesn’t work, there are two incomplete for loops and the lines in the parentheses don’t make sense

Nevermind, I figured out what you were trying to do, you probably just made a typo and accidentally wrote do instead of in pairs

There is one problem, however, it only loops through the first line for some reason

local lines = string.split(code, '\n')

for _,line do (lines) do
	local args = string.split(line,':')
    if not args[2] == nil then
	   newargs = string.gsub(line, '"', "")
    end
end

Im acctually making the same code executor currently using VM (it works for me)