How’s my loadstring module?

This is based on Adonis’s loadstring. However, I just felt like it would be better to make it into a separate module and make it simpler.

-- MainModule.lua

local Module = {}
local ExecuteScripts = setmetatable({}, {
	__index = function(self, key)
		_G.ExecuteScripts = self
		return self[key]
	end;
	__newindex = function(self, key, value)
		self[key] = value
		_G.ExecuteScripts = self
	end;
})

_G.ExecuteScripts = ExecuteScripts

Module.GetRandom = function(Len)
	local Length = (type(Len) == "number" and Len) or math.random(5,10)
	local Result = {}
	for Number = 1, Length do
		Result[Number] = string.format('%02x', math.random(126));
	end
	return table.concat(Result)
end

Module.Loadstring = function(String, Environment)
	return require(script.Loadstring:Clone())(String, Environment) -- Using a Loadstring Module
end

Module.Bytecode = function(Code)
	local _, Buffer = Module.Loadstring(Code)
	return Buffer
end

Module.ExecuteScript = function(Type, Code)
	if not Code and Type then
		return
	end

	local Function = Module.Bytecode(Code)
	local Key = Module.GetRandom()

	local Script
	if script:FindFirstChild(Type .. "Base") then
		Script = script:FindFirstChild(Type .. "Base"):Clone()

		local Execute = Instance.new("StringValue")
		Execute.Name = "Execute"
		Execute.Value = Key
		Execute.Parent = Script
	else
		warn('Error: Type of Script: "' .. Type .. '" does not exist.')
		return
	end

	ExecuteScripts[Key] = Function
	return Script
end

return Module;
-- ScriptBase.lua and LocalScriptBase.lua

wait()
local Execute = script:FindFirstChild("Execute")
local Function = _G.ExecuteScripts[Execute and Execute.Value]

local Success, Error = pcall(function()
	Function()
end)

do
	if not Success then
		error("Executed Script Error: " .. Error)
	end
end

This module allows you to use loadstring without using the loadstring() function. I would like to know how to make this cleaner or anything I could do to improve it. Here is the loadstring module: https://github.com/Sceleratis/Adonis/tree/master/Server/Dependencies/Loadstring

4 Likes

Guys, can I get some feedback? I would really appreciate it.

What is it that you’re not satisfied with?
Also, a link to Adonis’s loadstring would greatly help us help you.
What have you done to improve the code you provided, and what would you like to improve (if anything) ?

We’re here to help, but we need more details, check out this post on how to properly format your post and what to include: About the Code Review category

1 Like