I’m trying create a avatar editor and the accessory removal system doesn’t work with the accessories the player is wearing when they first join the game (it still works with accessories imported by the player afterwards tho).
This is the server script thats supposed to delete the accessory from the HumanoidDescription:
local HumanoidDescription = player.Character.Humanoid:GetAppliedDescription()
if string.find(HumanoidDescription.FaceAccessory, ID) then --- asset ID
string.gsub(HumanoidDescription.FaceAccessory, ID) --- doesn’t remove the ID from the string
print(HumanoidDescription.FaceAccessory) --- this still prints the same HumanoidDescription.FaceAccessory
end
I’ve tried to use both string.gsub and string.sub and it still doesn’t work with the accessories the player joins the game with, but works for other accessories
You’re not telling it to do anything. string.gsub takes three arguments. The third being what you’re replacing. Provide an empty string to remove the targeted component:
local text = "Hello, world!"
print(string.gsub(text, "Hello, ", "")) --> world! 1
You also need to re-assign HumanoidDescription.FaceAccessory to the result of your string.gsub call
The issue you are encountering comes from a misunderstanding of the purpose of sub and gsub. The shorthand “sub” stands for substitution and not subtraction. Additionally, strings are immutable, and you must use the return values of sub and gsub in order to see what they do.
More importantly, I cannot understand the purpose of your code. The FaceAccessory is already an ID, is it not? Wouldn’t the code then be equivalent to making the ID an empty string?
I don’t know if this causes any issues, but consider the case where the first ID in the CSV string needs to be removed, that call will do something like this: valA,valB,valC → ,valB,valC
It might be more reliable to do something like this:
local idTable = string.split(humanoidDescription.FaceAccessory, ",")
local foundIndex = table.find(idTable, ID)
if foundIndex then table.remove(idTable, foundIndex) end
humanoidDescription.FaceAccessory = table.concat(idTable, ",")