Ingame code creation with custom commands (code parsing issues)

What do you want to achieve? Ingame code creation with custom commands.

What is the issue? It doesnt get through the if statements

function run(code)
	for line in code:gmatch("([^\n]*)\n?") do
		local args = split(line)
		
		if args[1] == 'print' then
			local splitData = {splitByChar(args[2])}
			
			if splitData[1] == [[']] then
				print('as')
				if splitData[#splitData-1] == [[']] then
					print('as')
					local text = args[2]
					text = text:gsub([[']],'')
					print(text)	
				end
			end
			
			if splitData[1] == [["]] then
				print('as')
				if splitData[#splitData-1] == [["]] then
					print('as')
					local text = args[2]
					text = text:gsub([["]],'')
					print(text)	
				end
			end
		end
	end
end

function split(lineOfCode)
	local args = {}
	for word in string.gmatch(lineOfCode,[[([^(),]+)]]) do
		table.insert(args,#args+1,word)
	end
	
	return args
end

function splitByChar(str)
	local r = {}
	for a = 1, string.len(str)+1 do
		table.insert(r,#r+1,string.sub(str,a,a))
	end
	return r
end

game:GetService('ReplicatedStorage'):FindFirstChild('RunCode').OnServerEvent:Connect(function(plr,code)
	run(code)
end)
1 Like

Insufficient information.
You can also use the debug feature to verify that the values and functions are returning the correct information.

You can run custom code during a game using the loadstring method. It allows you create and run code during a game. Here is an already existing post that I’m sure answers your question.

Here is the Roblox definition of loadstring:

Which you can find here:
https://developer.roblox.com/en-us/api-reference/lua-docs/Lua-Globals

1 Like

I would assume you need to use loadstring however, allowing loadstring in settings makes your game easier to hack. I would recommend avoiding that, If you hit f9 and then hit the server button you can run commands without loadstring.

I do not want to make lua Im making something else with a similiar syntax to lua

And what will be the difference in syntax?
Could you explain what do you mean by it doesn’t go through the if statement?

It will be most of the time like functions example:
kill(plrName)
teleport(plr1,plr2)
randomPlr()

And variables example:
var int var1 = 50
var bool var2 = false

And if statements:
if variable ~= 49 then {
kill(‘PatriikPlays’)
teleport(‘PatriikPlays’,randomPlr())
}

Like a admin commands game but with code.

You created a new table with the first element as a table created by the splitting the string. You don’t need the extra {}.

local splitData = splitByChar(args[2])

Is there any particular reason for this over str:split("")?


You need to create a lexer and a parser for this as it looks like you’re trying to create a simple programming language. e.g. tokenise the code. e.g. print("hello world") into

identifier print
operator (
string "hello world"
operator )

Are statements terminated just by line breaks? Is semicolon to terminate a statement in the code allowed?
Are whitespaces between parenthesis allowed?
Is var required to declare a variable, or int var1 = 50 is just fine? And about int, is it 32-bit, 64-bit or arbitary precision? Is it statically typed or dynamically typed or both?
Is there any reason for braces after then over if (bool) {} or if bool then end? Is then required?
What are the reserved keywords? Same as Lua 5.1?
Are functions also first class like Lua? Can I assign kill, teleport and randomPlr to another value?
Is there anything else aside from functions, variables and if statements? Operators? Metatables?

Is the code creation only available to you or certain users, or is it available to everyone?

May I ask, why do you need this for admin commands?

Are statements terminated just by line breaks? Is semicolon to terminate a statement in the code allowed? Currently line breaks but I might change it to semicolons.

Are whitespaces between parenthesis allowed? Yes.

Is var required to declare a variable, or int var1 = 50 is just fine? It is required to declare a variable. If it is a var its read and write. If its a const it cant be written to after declaring the variable

And about int , is it 32-bit, 64-bit or arbitary precision? 64-bit

Is it statically typed or dynamically typed or both? Statically typed.

Is there any reason for braces after then over if (bool) {} or if bool then end ? Is then required? Then was just a idea but I think I dont want it.

What are the reserved keywords? Same as Lua 5.1? Same as lua 5.1.

Are functions also first class like Lua? Yes.

Can I assign kill , teleport and randomPlr to another value? Yes.

Is there anything else aside from functions, variables and if statements? Operators? Metatables? Yes. Operators.

Is the code creation only available to you or certain users, or is it available to everyone? There will be a game with a limited version of it like a free admin commands game and I will use the not limited version on my games as admin commands.

May I ask, why do you need this for admin commands? More powerful then normal chat admin commands.