Help with replacing strings?

Hi!
I’m trying to do a command (the command bar) to replace the decrepated “:connect()” to “:Connect()” on all scripts, but isn’t working.
I think the issue lies on source:gsub() but my mind is just blank.

local function findScripts(parent:DataModel)
	for _, child in ipairs(parent:GetDescendants()) do
		if child:IsA("BaseScript") then
			local source = child.Source
			local newSource, numReplacements = source:gsub(":connect(", ":Connect(")
            if numReplacements > 0 then
				script.Source = newSource
				print("Replaced " .. numReplacements .. " occurrences of :connect() with :Connect() in " .. script:GetFullName())
			end
		end

	end

	
end


local allScripts = findScripts(game)



print("Script replacement complete.")
2 Likes

You’re modifying the current script’s source

1 Like

This is very true, and to amend this I’ve changed the code so it should work and be more efficient, like removing :DataModel() from Parent because it doesn’t seem necessary. You should replace game.ServerScriptService with the correct parent as needed :)

local function findScripts(parent)
    for _, child in ipairs(parent:GetDescendants()) do
        if child:IsA("BaseScript") then
            local source = child.Source
            local newSource, numReplacements = source:gsub(":connect(", ":Connect(")
            if numReplacements > 0 then
                child.Source = newSource
                print("Replaced " .. numReplacements .. " occurrences of :connect() with :Connect() in " .. child:GetFullName())
            end
        end
    end
end

local parent = game.ServerScriptService -- Replace with the appropriate parent where your scripts are located
findScripts(parent)

print("Script replacement complete.")

Hope this helps you!!1!

edit: I meant to reply to omega, but it really doesn’t matter.

2 Likes