Hello, I’m trying to make a plugin that needs to get the first line of a script. Any help?
Why not use ModuleScripts, and return a function that passes the first line of the script? Would that work?
local firstLine = string.match(theScript.Source, "[^\n]*")
print(firstLine)
Or to skip past all the whitespace at the front:
local firstLine = string.match(theScript.Source, "%s*([^\n]*)")
Also just to do due diligence:
Why do you want to do this in the first place?
Okay that works, but how would I get more lines? (Sorry, I’m bad at scripting)
Can I ask why you need to do this in the first place? There might be a better solution than what I would answer.
It’s meant to obfuscate scripts, and I’m trying to get individual lines
Ok, so are you actually asking how to loop through all the lines in the script?
Also lua doesn’t care about newlines, so you could/should just treat the whole script as a single line if you’re trying to parse it.
Okay, lemme try that
(3O characters)
Sorry, not sure how to do this.
Heres the script:
script.Parent.MouseButton1Down:Connect(function()
local Selection = game:GetService("Selection")
for _, object in pairs(Selection:Get()) do
if object:IsA("Script") then
local str = object.Source
local scr = Instance.new("Script",workspace)
scr.Name = "ObfuscatedCode"
str = str:lower()
if str:find("local") then
print("found it in string")
end
local firstLine = string.match(object.Source, "[^\n]*")
-- table
local letters = {"I","l","l","L","i"} -- etc.
local length = 25
local shortcut = ""
for i = 1,length do
shortcut = shortcut.. "" ..letters[math.random(1,#letters)]
if i == length then
end
end
scr.Source = scr.Source .. [[
local ]]..shortcut..[[ = ]]..firstLine..[[
]]
else
print("Not a script")
end
end
end)
How to do what? What is this script supposed to do?
I just want to add lines individually
Supposed to turn
print("YELLO")
Into this:
local IlIlLillllIlLLllllLiLIlLi = print("YELLO")
Only does one line currently
(3O chars)
You can iterate through lines like this:
for line in string.gmatch(object.Source, "[^\n]*") do
print("'" .. line .. "'")
end
See String Patterns for more.
You might want to look at string.gsub if you’re just trying to find+replace, though.
However, have you thought about what happens if you give your plugin a script that looks like this?
print("a") print("b") local x = "c" print(x) function dosomething() print("d") end dosomething()
That’s valid lua, with no line breaks.
I don’t think your approach of just looking at the strings will get you very far. Give it a shot if you want to, though.
A more sophisticated system might parse the code into something called an abstract syntax tree and do operations on the code’s structure, instead of its text.