String.gsub acting weird

I want to make an r6 to r15 plugin, and I need to replace text in a string.

The issue is it is spamming replaced text, giving me a string too long error.

I have looked into many examples on how to use string.gsub, but none of them work

Here is my code
if object:IsA("Script") or object:IsA("LocalScript") or object:IsA("ModuleScript") then local ns = string.gsub(object.Source,"Torso", "HumanoidRootPart") print("ran1") ns = string.gsub(ns,"Left Arm", "LeftHand") print("ran2") ns = string.gsub(ns,"Right Arm", "RightHand") print("ran3") ns = string.gsub(ns,"Left Leg", "LeftFoot") print("ran4") ns = string.gsub(ns,"Right Leg", "RightFoot") print("ran5") ns = string.gsub(ns,'[Neck]', ".Neck") print(ns) print("ran6") ns = string.gsub(ns,".Neck", ".Parent.Head.Neck") print("ran7") ns = string.gsub(ns,'["Right Shoulder"]', ".Parent.RightUpperArm.RightShoulder") print("ran8") ns = string.gsub(ns,'["Left Shoulder"]', ".Parent.LeftUpperArm.LeftShoulder") print("ran9") ns = string.gsub(ns,'["Right Hip"]', ".Parent.RightUpperLeg.RightHip") print(#ns) ns = string.gsub(ns,'["Left Hip"]', ".Parent.LeftUpperLeg.LeftHip") print("ran11") print(#ns) local obj = object:Clone() obj.Source = ns obj.Name = object.Name .. "(R15)" obj.Parent = object.Parent print("ran12") end

Please post your code in a code-block.
You can format your code properly by putting a triple backtick before it (```) and after it, like so:

if object:IsA("Script") or object:IsA("LocalScript") or object:IsA("ModuleScript") then
	local ns = string.gsub(object.Source,"Torso", "HumanoidRootPart")
	print("ran1") ns = string.gsub(ns,"Left Arm", "LeftHand")
	print("ran2")
	ns = string.gsub(ns,"Right Arm", "RightHand")
	print("ran3")
	ns = string.gsub(ns,"Left Leg", "LeftFoot")
	print("ran4")
	ns = string.gsub(ns,"Right Leg", "RightFoot")
	print("ran5")
	ns = string.gsub(ns,'[Neck]', ".Neck")
	print(ns)
	print("ran6")
	ns = string.gsub(ns,".Neck", ".Parent.Head.Neck")
	print("ran7")
	ns = string.gsub(ns,'["Right Shoulder"]', ".Parent.RightUpperArm.RightShoulder")
	print("ran8")
	ns = string.gsub(ns,'["Left Shoulder"]', ".Parent.LeftUpperArm.LeftShoulder")
	print("ran9")
	ns = string.gsub(ns,'["Right Hip"]', ".Parent.RightUpperLeg.RightHip")
	print(#ns)
	ns = string.gsub(ns,'["Left Hip"]', ".Parent.LeftUpperLeg.LeftHip")
	print("ran11")
	print(#ns)
	local obj = object:Clone()
	obj.Source = ns
	obj.Name = object.Name .. "(R15)"
	obj.Parent = object.Parent
	print("ran12")
end

The problem here is that you’re using brackets, which are a special character when used in string patterns. You’ll have to escape the brackets using the % sign. Basically, anything in brackets is a “set”, and a pattern will match anything in a set instead of needing everything to be in the set.

Here’s an example of using sets:

local myString = "[hello]"
local myPattern = "[hello]"
local myPattern2 = "%[hello%]"

print(myString:gsub(myPattern, "whoops! ")) --> "[whoops! whoops! whoops! whoops! whoops! ]", 5
print(myString:gsub(myPattern2, "goodbye.")) --> "goodbye.", 1
4 Likes