String.gsub not working for replacing a code block

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Too be able too make this script working again.
  2. What is the issue? Include screenshots / videos if possible!
    This used to work on large script replacements, but now it doesn’t apparently.
    image
local services = {}
for i,v in pairs(game:GetChildren()) do
	pcall(function()
		services[#services + 1] = game:GetService(v.Name)
	end)
end

for i,service in pairs(services) do 
	for i,v in pairs(service:GetDescendants()) do
		pcall(function()
			if v:IsA('LuaSourceContainer') then
				local newScript = string.gsub(v.Source, 'while true do 
					script.Parent.Y.Color=Color3.new() 
					script.Parent.R.Color=Color3.new(1,0,0) 
					wait(22.05) 
					script.Parent.Y.Color=Color3.new() 
					script.Parent.R.Color=Color3.new() 
					script.Parent.G.Color=Color3.new(0,1,0) 
					wait(17.5)
					script.Parent.G.Color=Color3.new() 
					script.Parent.Y.Color=Color3.new(1,1,0) 
					wait(2.55)  
			end 
			', 'while true do 
				script.Parent.R.SurfaceLight.Enabled = false
				script.Parent.Y.Color=Color3.new() 
				script.Parent.R.Color=Color3.new() 
				script.Parent.G.SurfaceLight.Enabled = true
				script.Parent.G.Color=Color3.new(0,1,0) 
				wait(17.5)
				script.Parent.G.SurfaceLight.Enabled = false
				script.Parent.G.Color=Color3.new() 
				script.Parent.Y.SurfaceLight.Enabled = true
				script.Parent.Y.Color=Color3.new(1,1,0) 
				wait(2.55)  
				script.Parent.Y.SurfaceLight.Enabled = false
				script.Parent.Y.Color=Color3.new() 
				script.Parent.R.Color=Color3.new(1,0,0)
				script.Parent.R.SurfaceLight.Enabled = true
				wait(22.05) 
			end 
			')
				v.Source = newScript
			end
		end)
	end 
end

Original script that’s used to originally get all scripts that used the same scripting and replace those scripts with a new code, this would be put into the command bar.

local services = {}
for i,v in pairs(game:GetChildren()) do
    pcall(function()
        services[#services + 1] = game:GetService(v.Name)
    end)
end

for i,service in pairs(services) do 
    for i,v in pairs(service:GetDescendants()) do
        pcall(function()
            if v:IsA('LuaSourceContainer') then
                local newScript = string.gsub(v.Source, 'CodeHere', 'replacecodewithwhateverhere')
                v.Source = newScript
            end
        end)
    end 
end
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

No solutions, roblox broke it.

Originally referring back to this post I’ve made in the past:

2 Likes

Roblox didn’t broke it, you broke it.

The way you typed the string isn’t valid. Also, the end keyword became a string, thus not making the whole scope end.

Actually, it used to work with a large sum of scripts that had the same coding of whatever, and it would work if I replaced the same codings in each script to a different coding, ROBLOX did break it.

The real question here is: why are you trying to change a source code?

  1. You can’t change a source code of a script using other scripts except plugins.
  2. The problem isn’t in the script, it’s in another script located in Traffic Light - set A.
  3. There are no problems with the current script you stated. The error happens somewhere else.

Don’t blame Roblox if something broke. Changes are expected for stability and performance increase. Don’t jump into conclusion.

gsub is meant to be used with patterns not plaintext. Roblox didn’t break anything here, you were incorrectly coding and believed because your previous cases worked that the way you used them was correct when it really wasn’t.

It really is an easy fix but not the way you’re doing it: you have two different and better options.

  1. Use packages instead.
  2. Use one script to control all objects which have the same code in them, instead of writing one script then copying and pasting it into several other similar ones. CollectionService will help you for this; tag all light posts, then that one script will automatically handle their behaviour.

Not enough context so I don’t fully understand your circumstances but if the assumptions I’m making are holding true, then these are better than fumbling around with your incorrect use of functions.

To begin with, even if you weren’t using these two methods, you should know what replacements you’re making, so why not just directly write to the Source property instead of using gsub?

1 Like

I did the same steps previously in the past for the current code, and it doesnt work, roblox did intend to break it.

I’m not really sure what you mean by this post. What steps and for what? Please be more specific. If your code doesn’t work, then you should also be debugging it to check why it isn’t.

As for “Roblox did intend to break it”, again, no they didn’t. string.gsub comes directly from vanilla Lua with some potential technical changes for the platform. They don’t have intentions to break anything ever. This was your own misuse and misunderstanding of gsub that’s making it “not work”.

1 Like

No, you guys don’t really understand the post I’ve made in the past, it would work with the provided script the user @7z99 has written, the fact it used to work for replacing codes with the same coding to a new coding.

For ex, I want to replace all scripts that contained:

game.ReplicatedStorage.

And change it too:

game.ReplicatedStorage. = Replicated.

I would then put it as:

local services = {}
for i,v in pairs(game:GetChildren()) do
    pcall(function()
        services[#services + 1] = game:GetService(v.Name)
    end)
end

for i,service in pairs(services) do 
    for i,v in pairs(service:GetDescendants()) do
        pcall(function()
            if v:IsA('LuaSourceContainer') then
                local newScript = string.gsub(v.Source, 'game.ReplicatedStorage.', 'game.ReplicatedStorage = Replicated.')
                v.Source = newScript
            end
        end)
    end 
end

After executing the script above in my command bar, it would change all codes containing exactly game.ReplicatedStorage. with 'game.ReplicatedStorage = Replicated.

However, this no longer works for replacing big chunk of codes, as it used to in the past.

I didn’t read your past post because it’s not relevant to me. What’s relevant to me is this post and its problem and the fact that you’re refusing to look at proper solutions though having being given them.

I’ve already pointed out a few times that you’re incorrectly using gsub and you’re spending too much time on this problem when there are proper solutions available that you’re refusing to use, though they would fix this case immediately.

Again: string.gsub is intended to work with patterns, not plaintext replacements. Even if you catch a few exception cases where a plaintext pattern would work, I have no clue why you’re trying to gsub an entire code chunk. This is just completely bad practice and a real waste of time.

Your example case is very, very different from what you’re doing in the OP. Because you don’t understand how gsub or string patterns work, you have a ton of oversights already present just in the string you’re feeding to gsub, especially in cases of using special characters.

And again: you could be done with this problem if you just used a proper solution here. Instead of trying to fumble over automating code replacement for as many hour as you have, switching to a packaged script/model or using a single script to handle your items of the same type would both resolve your issue and allow you to deploy a better, more performant coding practice.

This can just be a few minutes job to use proper solutions over insisting on automating something that’s fundamentally erroneous to begin with. Roblox did not break gsub, you don’t know how to use it and are insistent that it’s not your oversight because it worked on a small chunk in the past.

Please brush up on string manipulation and string patterns via the Developer Hub.

It’s okay, you don’t understand what my past post reference is, the fact you state it’s irrelevant meaning you don’t understand anything of what I’m saying, therefore, refrain from speaking on my topic.

The character . represents all characters and since the second parameter of gsub takes a string pattern, it will read it as a pattern

try this:

local services = {}
for i,v in pairs(game:GetChildren()) do
    pcall(function()
        services[#services + 1] = game:GetService(v.Name)
    end)
end

for i,service in pairs(services) do 
    for i,v in pairs(service:GetDescendants()) do
        pcall(function()
            if v:IsA('LuaSourceContainer') then
                local newScript = string.gsub(v.Source, 'game%.ReplicatedStorage%.', 'game.ReplicatedStorage = Replicated.')
                v.Source = newScript
            end
        end)
    end 
end

or

local services = {}
for i,v in pairs(game:GetChildren()) do
    pcall(function()
        services[#services + 1] = game:GetService(v.Name)
    end)
end

for i,service in pairs(services) do 
    for i,v in pairs(service:GetDescendants()) do
        pcall(function()
            if v:IsA('LuaSourceContainer') then
                v.Source = v.Source:gsub('game%.ReplicatedStorage%.', 'game.ReplicatedStorage = Replicated.')
            end
        end)
    end 
end
1 Like

You included your previous post right on the thread and it’s related to the code you posted; that’s not what I’m failing to understand. What I’m not understanding is why you’re adamantly refusing to settle for a better solution from a technical and maintenance perspective and why you prefer to continue incorrectly using gsub and rather spend hours trying to fix something that’s inherently incorrect instead of getting this done a quicker and proper way.

Fella, the support categories are for getting help and that’s what I’m trying to offer here, while also pointing out that you don’t understand how to use string.gsub for its intended purpose. I’ve explained why it’s wrong and pointed out the resources you can reference to brush up on your understanding about them. In addition, I gave you choices that would better resolve your problem not only by making this easier but by improving the way you handle this system.

There is really no point in trying to automate the entire replacement of code this way and you’re still not listening to my advice in any capacity, instead dismissing it on the basis of me (for good reason) ignoring your previous post because it’s not actually relevant to offering a solution to the problem. gsub works in terms of patterns. It may have worked for a simple replacement of game’s capitalisation, but it’s primary intention is to replace text based on a pattern. An entire script is not a pattern and it’s even triggering special characters reserved for pattern matching.

When I offer help I focus on problems not solutions.

3 Likes