Automatically Insert Commas

EDIT: I have made changes to the plugin because it was not working. I had the structure default to 1 which was incorrect and I have also removed the unnecessary currentScript:IsA("Script"). Thanks for all your support!

EDIT2: I have made changes to the plugin so now it uses events instead of the while wait loop. This is highly more efficient and the plugin now instaly inserts commas instead of the 0-0.1 second wait. Thanks again for all of your support and suggestions!

Introduction

Hello developers, today as I was browsing the DevForum, I came across a feature request that I thought I could make. The idea was simple, automatically putting commas in tables. I didn’t think it would be complicated so I decided to give it a go.

After like an hour of work, I had a working plugin and I decided to put it on here.

Demonstration
I should mention that you cannot delete the comma. If the plugin detects there should be a comma then it will continously put one in the script.

The Comma Preview

How it works
To dumb it down it bassically loops through each line. On a line, it will check if it has any brackets. If it does, then it will loop through each character. In the loop it will check each characters value. If the character is a “{” then it will increase the structure value by one. If it is a “}”, it will decrease. When it comes time to put the line together, it will check if it should either add a comma and an newline or just a newline. The check returns a comma if the bracket == “}” and the structure is over 0.

Code

game.StudioService.Changed:Connect(function()
	local currentScript = game.StudioService.ActiveScript
	connection = (connection and connection:Disconnect()) or currentScript and currentScript:GetPropertyChangedSignal("Source"):Connect(function()
		local output = ""
		local structure = 0
		if task.wait() and currentScript then
			local lines = currentScript.Source:split("\n")
			for i,v in pairs(lines) do
				local bracket,pos = v:gsub("	","")
				if string.find(v,"{") or string.find(v,"}") then
					for i,char in pairs(string.split(v,"")) do
						structure = char == "{" and structure + 1 or char == "}" and structure - 1 or structure
					end
				end
				output = output..v..tostring((bracket == "}" and structure > 0) and ",\n" or tostring(i ~= #lines and "\n" or ""))
			end
			currentScript.Source = output
		end
	end)
end)

Conclusion
In all, it inserts commas. But I don’t reccommend this plugin because I did not test it with many situations. But, from the situations I did test, it never had a problem.

I made a plugin install if you want, but I literally made this in about an hour so there might be bugs.

7 Likes

Very cool, having a full autocomplete system would be interesting.

I would suggest making a table with all scripts types:

local scripts = {"Script", "LocalScript", "ModuleScript"}

and change

with table.find(scripts, currentScript.ClassName) so that the plugin works in all scripts types.

OR just do currentScript:IsA("LuaSourceContainer")

4 Likes

@NinjaFurfante07 You can read about that here.

1 Like

Thank you for this plugin, I always found adding commas annoying when doing a large data table. Thank you so much!

Thanks for letting me know, i wasn’t aware of that

1 Like

NOOOOOOOO

Always use Event-Based programming even with plugins. Polling is the worst code-smell

AutoComma2.lua (1.1 KB)

Put in %localappdata%/Roblox/Plugins and restart Studio for those wondering

1 Like

Hello, I didn’t want to use a while wait loop. My only solution would be to get selection changed. Then get property changed signal from source. I’ll try and see if it works.

This is nice, but I feel like we should be able to remove the commas if we want. Don’t know why, but maybe some people don’t want commas for a specific reason. Imagine if autocorrect was like that.

I’m aware of the issue but unfortunately, there isn’t really a solution as far as I can see. If you have any ideas lmk but I have tried to make it so you can remove the commas, I just can’t figure it out.

1 Like

But what I need is manual activation.