How to delete letters from a string

Hello I want to remove a value from a string

code:

local LuckData:StringValue = UserData:WaitForChild("Luck")
	local i=0
	for numberStr in string.gmatch(LuckData.Value, "[^,%s]+") do
		if tonumber(numberStr)==2 then  
			local startPos, endPos = string.find(LuckData.Value, i)
			local sb = string.sub(LuckData.Value,startPos,endPos )
			LuckData.Value = sb
			print(sb)
			return
		end

	end

The Value of LuckData is “2, 4, 2”, i want it to be “4, 2” how do i do that

Please Help Me

Thank you :heart:

You can try string.split

local LuckData:StringValue = UserData:WaitForChild("Luck")
	local i=0
	for numberStr in string.gmatch(LuckData.Value, "[^,%s]+") do
		if tonumber(numberStr)==2 then  
			local split = string.split(LuckData.Value,",")
            local sb = tostring(split[2])..", "..tostring(split[3])
			print(sb)
			return
		end

	end

edit: i tested with command bar and it worked

i changed the value a bit into a module

function module.removeluck(num:number)
	local UserData = SessionData:WaitForChild(tostring(Player.UserId))
	local LuckData:StringValue = UserData:WaitForChild("Luck")
	local i=0
	for numberStr in string.gmatch(LuckData.Value, "[^,%s]+") do
		if tonumber(numberStr)==num then  
			local split = string.split(LuckData.Value,",")
			local sb = tostring(split[2])..", "..tostring(split[3])
			print(sb)
			return
		end

	end
	
end

when the value is “2, 4, 2” it changes into “4, 2”, but when the value is “2, 4” it changes to “4, 2” it flips idk why

Sorry for the late reply, but it flips when there are only 2 because its made for 3 splits.

tostring(split[2])..", "..tostring(split[3]) -- see numbers [2] and [3]

but for 2 numbers you can take away 1 from each split

tostring(split[1])..", "..tostring(split[2]) -- see numbers [1] and [2]

Sorry if I misunderstood the question, but you can use string.gsub to remove characters from a string.

local Str = "Hello world"
local Str2 = string.gsub(Str, "Hello", "")
print(Str2) --> " world"