Automorph Script Welds and Repeating

Hello,

I have an issue with my automorph script. Essentially, it creates anywhere from 5-50 welds where it should only create 1 to a few, and it seems to duplicate the person’s morph every time they die. This obviously results in major performance issues and especially so with multiple people. Was hoping someone would be able to either fix or let me know how to fix the script.

local morphsFolder = game.ReplicatedStorage._library._morphs
local Players = game.Players

function loadMorph(player,morph,primarypart)
	wait(0.05)
	local morph_duplicate = morph:Clone()
	morph_duplicate.PrimaryPart = morph_duplicate.Middle
	morph_duplicate:SetPrimaryPartCFrame(primarypart.CFrame)
	morph_duplicate.Parent = player.Character.Morph
	for i,v in pairs (morph_duplicate:GetChildren()) do
		if v.className ~= "LocalScript" then
			player.Character.Humanoid:RemoveAccessories()
			local W = Instance.new("Weld")
			W.Part0 = morph_duplicate.Middle
			W.Part1 = v
			local CJ = CFrame.new(morph_duplicate.Middle.Position)
			local C0 = morph_duplicate.Middle.CFrame:inverse()*CJ
			local C1 = v.CFrame:inverse()*CJ
			W.C0 = C0
			W.C1 = C1
			W.Parent = morph_duplicate.Middle
		end
		local W2 = Instance.new("Weld")
		W2.Part0 = primarypart
		W2.Part1 = morph_duplicate.Middle
		W2.Parent = primarypart
		for i,v in pairs (morph_duplicate:GetChildren())do
			if v.className ~= "LocalScript" then
				v.Anchored = false
				v.CanCollide= false
			end
		end
		if player.Character:FindFirstChildOfClass("Shirt") or player.Character:FindFirstChildOfClass("Pants") then
			for _, child in pairs(player.Character:GetChildren()) do
				if child.className == "Shirt" or child.className == "Pants" then
					child:Destroy()
				end
			end
		end
		local shirt = morph.Parent.Shirt:Clone()
		local pants = morph.Parent.Pants:Clone()
		shirt.Parent = player.Character
		pants.Parent = player.Character
		for _, child in pairs(player.Character:GetChildren()) do
			if child.className == "Accessory" then
				child:Destroy()
			end
		end
	end
	if morph_duplicate:FindFirstChild("clientNVG") then
		wait(1)
		morph_duplicate.clientNVG.Parent = player.PlayerGui
		player.PlayerGui:FindFirstChild("clientNVG").Disabled = false
	end
	if morph.Parent.Name == "AGENT" then
		player.Character:FindFirstChild("Body Colors"):Destroy()
		player.Character.Head.face:Destroy()
		for _, bodyPart in pairs(player.Character:GetChildren()) do
			if bodyPart.className == "MeshPart" or bodyPart.className == "Part" then
				bodyPart.BrickColor = BrickColor.new("Really black")
			end
		end
	end
	if morph.Parent.Name == "DELTA-4" or morph.Parent.Name == "OFFICER" or morph.Parent.Name == "PERSONNEL" or morph.Parent.Name == "NCO" then
		for _, bodyPart in pairs(player.Character:GetChildren()) do
			local bc = player.Character:FindFirstChild("Body Colors")
			bc.TorsoColor = BrickColor.new("Really black")
		end
	end
	if morph.Parent.Name == "HAZMAT" then
		for _, bodyPart in pairs(player.Character:GetChildren()) do
			local bc = player.Character:FindFirstChild("Body Colors")
			bc.TorsoColor = BrickColor.new("Really black")
			bc.HeadColor = BrickColor.new("Really black")
		end
	end
end
local morphs = {
	["Mobile Task Forces"] = {
		{"greaterThanequalTo",1,"DELTA-4"},
	},
	["Engineering and Technical Services"] = {
		{"greaterThanequalTo",255,"HAZMAT"},
		{"greaterThanequalTo",7,"CE"},
		{"greaterThanequalTo",5,"SE"},
		{"greaterThanequalTo",1,"ETS"},
	},
	["Security Department"] = {
		{"greaterThanequalTo",6,"OFFICER"},
		{"greaterThanequalTo",4,"NCO"},
		{"greaterThanequalTo",1,"PERSONNEL"},
	},
	["Specialized Recontainment Unit"] = {
		{"greaterThanequalTo",6,"OFFICER"},
		{"greaterThanequalTo",1,"PERSONNEL"},
	},
}
game.Players.PlayerAdded:Connect(function(player)
	wait(0.05)
	player.CharacterAdded:Connect(function()
		player.CharacterAppearanceLoaded:Connect(function(character)
			for x,z in pairs(morphs) do
				for i,v in pairs(z) do
					if player.Team.Name == x then
						if v[1] == "greaterThanequalTo" and player:GetRankInGroup(game.Teams:FindFirstChild(x).groupId.Value) >= v[2] then
							for _, morphChild in pairs(morphsFolder:FindFirstChild(x):FindFirstChild(v[3]):GetChildren()) do
								if morphChild.className == "Model" and not player.Character.Morph:FindFirstChild(morphChild.Name) then
									loadMorph(player,morphChild,player.Character:FindFirstChild(morphChild.Name))
								end
							end
						elseif v[1] == "lessThanequalTo" and player:GetRankInGroup(game.Teams:FindFirstChild(x).groupId.Value) <= v[2] then
							for _, morphChild in pairs(morphsFolder:FindFirstChild(x):FindFirstChild(v[3]):GetChildren()) do
								if morphChild.className == "Model" and not player.Character.Morph:FindFirstChild(morphChild.Name) then
									loadMorph(player,morphChild,player.Character:FindFirstChild(morphChild.Name))
								end
							end
						elseif v[1] == "equalTo" and player:GetRankInGroup(game.Teams:FindFirstChild(x).groupId.Value) == v[2] or v[4] ~= nil and v[4] == player.Name  or v[5] ~= nil and v[5] == player.Name then	
							for _, morphChild in pairs(morphsFolder:FindFirstChild(x):FindFirstChild(v[3]):GetChildren()) do
								if morphChild.className == "Model" and not player.Character.Morph:FindFirstChild(morphChild.Name) then
									loadMorph(player,morphChild,player.Character:FindFirstChild(morphChild.Name))
								end
							end
						end
					end
				end
			end
		end)
	end)
end)
2 Likes