How to make a format script?

How would I go about making a format script that turns local variables and global variables in to the typeof() of what they equal eg

local a = 1
local b = ""
local c = workspace
a = 2
b = "1"
c = game.ReplicatedStorage
--turn that ^ Into this |
local number = 1
local string = ""
local Instance = workspace
number = 2
string = "1"
Instance = game.ReplicatedStorage

How would I do that please help!!

1 Like

The trouble is that when I use string.gsub then it always returns “String” so can someone help me?

1 Like

Just remember you can only write to the source through plugin scripts or the command bar.

Use some basic string manipulation:

--// something like

local source = workspace.Script
source.Source = source.Source:gsub("local%s-(%w+)%s-%=%s-(%S+)%s?", function(a, b)
    local  _, c = pcall(function()return game[b:match("game%p(%w+)") or b] and "Instance" end);
    if(tostring(c):match("is not a valid member")) then
        print(b, " is not a member of game")
        c = typeof(getfenv()[b] or tonumber(b) or b);
    end
    return "local "..c.." = "..b.."\n" 
end)

so the source of a script under game.Workspace with this code:

image

turns into:

image

Note however that my string manipulation is a bit rusty and I know it can be improved (I’ll leave you to that for now) for instance quotes within a string could cause it to malfunction (I don’t know anymore since I’ve changed the code a lot, but the point is you can make several improvements to this - I don’t have a lot of time on my hands right now so this is just a rudimentary solution).

Again you can expand on this pretty easily, or just use an easier alternative if applicable now that you’ve seen one possible method.

Other than that you haven’t really provided any information at all so we can’t fix your current system, if you’d still like to make your own implementation work so you can at the very least learn something, provide us your attempt.

1 Like

That’s just very useful thank you for the valuable information

1 Like