I have this character creation script. I finally got it working probably, or I thought so, until I met this very weird simple problem: Hairs are duplicating on eachother. I've had alot problems with the hairs, but 'cause I think I'm so close to the answer, and I want to get it done, I post this here.

My workspace is quite messy, so I'm not gonna even try to show it. I'll rather use comments in this script.

local character = game.Workspace["ROBLOX Character"] --the charcter to be customized

local player = script.Parent.Parent.Parent.Parent --the player

local data = require(game.ServerStorage.Data) --data store module for the game to save this character
local event = Instance.new("RemoteEvent",player.Backpack) -- creates button click event used to save the stats to data store
event.Name = "CharacterEvent"

local char = script.Parent:WaitForChild("Character")

local gender --temp gender value, updates when the gender is changed
local hair --same, but for hair
local hair_color --hair color
local skin --...
local face
local shirt
local pants

local current_hair = nil --the hair instance
local current_package = {} --table of character mesh instances
local current_face = nil --the face instance
local current_shirt = nil
local current_pants = nil

function load_appearance() --loads appearance once, doesn't remove anything
	local package = script.Gender[gender]:GetChildren() --get's a table of the character mesh instances, located in this script
	
	for i,body_part in pairs(package) do --applying the character meshes for the character
		local part = body_part:Clone()
		part.Parent = character
		table.insert(current_package,part)
	end
	
	local h = script.Hair[gender][hair]:Clone() --gets hair instance
	h.Parent = character
	current_hair = h
	
	local hair_part = h:FindFirstChild("Handle")
	if hair_part then
		hair_part.BrickColor = char["Hair Color"].Value --colors hair
	end
	hair_color = char["Hair Color"].Value
	
	for i,part in pairs(character:GetChildren()) do
		if part:IsA("BasePart") then
			part.BrickColor = char.Skin.Value --colors skin
		end
		skin = char.Skin.Value
	end
	
	local f = script.Face[face]
	local head = character:FindFirstChild("Head")
	if head then
		f.Parent = head
		current_face = f
	end
	
	local s = script.Shirt[gender][tostring(shirt)]:Clone() --tostring 'cause shirt is a number value
	s.Parent = character
	current_shirt = s
	
	local p = script.Pants[gender][tostring(pants)]:Clone() --same here
	p.Parent = character
	current_pants = p
end

function update_appearance() --removes old appearance, applies new
	
	if gender ~= char.Gender.Value and current_package ~= {} then
		
		if current_hair then --checks if old hair instance found
			current_hair:Destroy() --if so, removes it
			current_hair = nil
		end
		
		
		if current_shirt then
			current_shirt:Destroy()
			current_shirt = nil
		end
		
		
		if current_pants then
			current_pants:Destroy()
			current_pants = nil
		end
		
		
		for i,child in pairs(current_package) do
			child:Destroy()
			child = nil
		end
		current_package = {}
		
		shirt = char.Shirt.Value
		
		pants = char.Pants.Value
		
		gender = char.Gender.Value
		
		while not script.Hair[gender]:FindFirstChild(char.Hair.Value) do --wait for the hair instance to be found from the script correctly, this was one of the fixes I had to do to make this work properly
			wait()
		end
		
		hair = char.Hair.Value
		
		local h = script.Hair[gender][hair]:Clone()
		h.Parent = character
		current_hair = h 
		
		local s = script.Shirt[gender][tostring(shirt)]:Clone()
		s.Parent = character
		current_shirt = s
		
		local p = script.Pants[gender][tostring(pants)]:Clone()
		p.Parent = character
		current_pants = p
		
		local package = script.Gender[gender]:GetChildren()
	
		for i,body_part in pairs(package) do
			local part = body_part:Clone()
			part.Parent = character
			table.insert(current_package,part)
		end
	end
	
	if hair ~= char.Hair.Value and current_hair then
		current_hair:Destroy()
		current_hair = nil
		
		while not script.Hair[gender]:FindFirstChild(char.Hair.Value) do
			wait()
		end
		
		hair = char.Hair.Value
		
		local h = script.Hair[gender][hair]:Clone()
		h.Parent = character
		current_hair = h
	end
	
	if face ~= char.Face.Value and current_face then
		current_face:Destroy()
		current_face = nil
		
		face = char.Face.Value
		
		local f = script.Face[face]:Clone()
		local head = character:FindFirstChild("Head")
		if head then
			f.Parent = head
			current_face = f
		end
	end
	
	if shirt ~= char.Shirt.Value and current_shirt then
		current_shirt:Destroy()
		current_shirt = nil
		
		shirt = char.Shirt.Value
		
		local s = script.Shirt[gender][tostring(shirt)]:Clone()
		s.Parent = character
		current_shirt = s
	end
	
	if pants ~= char.Pants.Value and current_pants then
		current_pants:Destroy()
		current_pants = nil
		
		pants = char.Pants.Value
		
		local p = script.Pants[gender][tostring(pants)]:Clone()
		p.Parent = character
		current_pants = p
	end
	
end

function update_color() --updates color
	
	if current_hair then
		local hair_part = current_hair:FindFirstChild("Handle")
		if hair_part and hair_part.BrickColor ~= char["Hair Color"] then
			hair_part.BrickColor = char["Hair Color"].Value
		end
		
		if hair_color ~= char["Hair Color"].Value then
			hair_color = char["Hair Color"].Value
		end
	end
	
	for i,part in pairs(character:GetChildren()) do
		if part:IsA("BasePart") then
			if part.BrickColor ~= char.Skin.Value then
				part.BrickColor = char.Skin.Value
			end
			
			if skin ~= char.Skin.Value then
				skin = char.Skin.Value
			end
		end
	end
	
end

wait(1) --this is temporary, I'll fix all below when I get this working
gender = char.Gender.Value
hair = char.Hair.Value
hair_color = char["Hair Color"].Value
skin = char.Skin.Value
face = char.Face.Value
shirt = char.Shirt.Value
pants = char.Pants.Value
wait(1) --same here
load_appearance() --loads the start appearance

for i,v in pairs(char:GetChildren()) do --connection for everything below
	v.Changed:connect(function()
		update_appearance() --updates appearance
		update_color() --updates color
	end)
end

event.OnServerEvent:connect(function()
	data:reset(player) --problem not below
	local d = data:get(player)
	if d then
		local character_data = data:set(player,character,
			{
			["gender"] = gender,
			["hair"] = hair,
			["hair color"] = hair_color,
			["skin"] = skin,
			["face"] = face,
			["shirt"] = shirt,
			["pants"] = pants
			}
		)
		for k,v in pairs(character_data) do
			print(k)
		end
	end
end)

Thanks,

SwaggyDuckie

1 comment

0

245