Help using a string in Enum

Hi, I’m making a script that copies a character’s appearance and puts it in another rig. This segment of code copies the character’s charactermeshes, but it returns this error:

for i = 1, #children do
	local child = children[i]
	if child:IsA("CharacterMesh") then
		local id = child.MeshId
		local bodypart = tostring(child.BodyPart) 
					
		local new = Instance.new("CharacterMesh")
		new.Parent = clone
		new.MeshId = id
		new.BodyPart = Enum.BodyPart[bodypart]

	end
end

image

Enum.BodyPart.LeftLeg definitely exists, and if I change the code to just this it works perfectly fine.

new.BodyPart = Enum.BodyPart["LeftLeg"] 

I need to turn the BodyPart into a string because I’m planning to save them with data stores later, any help with this?

When you tostring() the body part, it returns “Enum.BodyPart.LeftLeg”, not just “LeftLeg”. Add a simple :gsub() to the string and have it remove all the unnecessary parts.

local bodypart = tostring(child.BodyPart):gsub("Enum.BodyPart.", "")

This right here, takes “Enum.BodyPart.LeftLeg” and replaces “Enum.BodyPart.” with “”(nothing).

1 Like

You could create a dictionary with every body part enum, and just refer to that.

Something like this:

local BodyParts = {
    ['LeftArm'] = Enum.BodyPart.LeftArm
}

-- and call it like this:

BodyParts['LeftArm']

If you’re double referencing Enum.BodyPart, aka your code is doing this: Enum.BodyPart[‘Enum.BodyPart.LeftArm’] then Tyler’s solution should work just fine.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.