Auto Morph System - Team Issue

Hello, so me and a friend have been a developing a new automorph script which will make it easier for people in need of an auto morph script.

What’s my issue you might be asking?

The morph system works when choosing which morph to assign to a player by using their playerid, however it fails when using the team, they are on and searching for their groupid before rank.

Configuration part of the code:

local morphHandler = {
	configuration = {
		["players"] = {
			[2007199827] = "Darth Shrek",
			[3149528904] = "Marr",
			[2658690346] = "Jadus",
			[1647057212] = "CoolServant",
			[1531759262] = "Naga",
		},
		
		["Sith"] = {
			[1] = "Initiate",
			[3] = "Initiate",
			[4] = "Initiate",
			[2] = "Apprentice",
			[6] = "Sith Knight",
			[10] = "Sith Warrior",
			[7] = "Sith Marauder",
			[15] = "Sith Lord",
			[14] = "Sith Blademaster",
			[11] = "Overseer",
			[255] = "Emperor",
		},
		["Council"] = {
		},
		["Powerbase"] = {
		},
		["HonorGuard"] = {
			[3] = "Guardsmen",
			[4] = "Guardsmen",
			[5] = "Guardsmen",
			[6] = "Shadow",
			[7] = "Sergeant",
			[8] = "Officer",
			[9] = "Commander",
			[254] = "Overseer",
			[255] = "Overseer"
		},
		["Inquisition"] = {
			[10] = "Inquisitor Initiate",
			[11] = "Inquisitors",
			[12] = "Senior Inquisitor",
			[25] = "Senior Inquisitor",
			[45] = "Senior Inquisitor",
			[65] = "Senior Inquisitor",
			[85] = "Senior Inquisitor",
			[105] = "Overseer1",
			[255] = "Overseer1"
		}
	}
}

How it works is it has the rank ID then the morphs name, and the team name is the top part named “Sith” for example.

This is the code for the actual morph giving part of the script:

local function findChildInDescendants(descendants, name)
	local ret:Instance? = nil

	for key, value in descendants do
		if value.Name == name then
			ret = value
			break
		end
	end

	return ret
end

local function addMorph(character, chosenMorph)
	if character:FindFirstChild("MorphContainer") then
		character.MorphContainer:Destroy()
		task.wait(0.25)
	end

	local morphContainer = Instance.new("Model", character)
	morphContainer.Name = "MorphContainer"

	if character:WaitForChild("Humanoid") ~= nil then
		for _, morphPart in pairs(chosenMorph:GetChildren()) do
			if morphPart.ClassName ~= "Folder" then
				local clone = morphPart:Clone()
				clone.Parent = morphContainer

				for key, value in pairs(clone:GetChildren()) do
					if value:IsA("Model") then
						for key, value in value:GetChildren() do
							value.Parent = clone
						end
					end

					local weld = Instance.new("Weld")
					weld.Part0 = clone.Middle
					weld.Part1 = value

					if value.Anchored == true then
						value.Anchored = false
					end

					local CJ = CFrame.new(clone.Middle.Position)
					local C0 = clone.Middle.CFrame:inverse()*CJ
					local C1 = value.CFrame:inverse()*CJ
					weld.C0 = C0
					weld.C1 = C1
					weld.Parent = clone
				end

				local weld = Instance.new("Weld")
				weld.Part1 = clone.Middle
				weld.Part0 = character[morphPart.Name]
				weld.C0 = CFrame.new()
				weld.Parent = clone

				for _, val in clone:GetChildren() do
					if val:IsA("MeshPart") or val:IsA("UnionOperation") or val:IsA("Part") then
						val.Anchored = false
						val.CanCollide = false
					end
				end
			end
		end

		for _, value in character:GetChildren() do
			if value:IsA("BasePart") or value:IsA("Part") then
				value.Transparency = 1
			end
		end

		task.spawn(function()
			for _, value in character:GetChildren() do
				if value:IsA("Accessory") or value:IsA("Hat") then
					value:Destroy()
				end
			end

			task.wait(3)

			if character:FindFirstChildOfClass("Accessory") ~= nil or character:FindFirstChildOfClass("Hat") ~= nil then
				for _, value in character:GetChildren() do
					if value:IsA("Accessory") or value:IsA("Hat") then
						value:Destroy()
					end
				end
			end
		end)
	end
end

local function addTools(player, chosenMorph)
	local tools = chosenMorph:FindFirstChild("Tools")

	if tools then
		for key, value in pairs(tools:GetChildren()) do
			if value:IsA("Tool") then
				value.Parent = player.Backpack
			end
		end
	end
end

local function findAppropriateMorph(player:Player)
	local team = player.Team

	local foundMorph = nil

	if team == game:GetService("Teams"):WaitForChild("Raiders") then
		foundMorph = findChildInDescendants(game:GetService("ReplicatedStorage"):WaitForChild("Morphs"):GetDescendants(), "Jedi")

		return foundMorph
	elseif team == game:GetService("Teams"):WaitForChild("Independant") then
		foundMorph = findChildInDescendants(game:GetService("ReplicatedStorage"):WaitForChild("Morphs"):GetDescendants(), "Civilian")

		return foundMorph
	end

	if team ~= game:GetService("Teams"):WaitForChild("Raiders") or team ~= game:GetService("Teams"):WaitForChild("Independant") then
		local teamId = game:GetService("Teams"):FindFirstChild(team.Name):FindFirstChild("GroupId").Value

		local appropriateMorph = nil

		local rank = player:GetRankInGroup(teamId)

		if morphHandler.configuration.players == nil then
			foundMorph = morphHandler.configuration[team.Name][rank]

		elseif morphHandler.configuration.players ~= nil then

			if morphHandler.configuration.players[player.UserId] ~= nil then
				foundMorph = morphHandler.configuration.players[player.UserId]

			elseif morphHandler.configuration.players[player.Name] == nil then
				foundMorph = morphHandler.configuration[team.Name][rank]
			end
		end

		print(foundMorph)
		print(appropriateMorph)
		print(rank)
		print(team)
		print(teamId)
		
		if foundMorph ~= nil then																													
			appropriateMorph = findChildInDescendants(game:GetService("ReplicatedStorage"):WaitForChild("Morphs"):GetDescendants(), foundMorph)
		end

		return appropriateMorph
	end
end

function morphHandler:applyMorph(player:Player)
	local chosenMorph = nil

	local playerData = game:GetService("ServerStorage"):WaitForChild("PlayerData")[player.Name]

	if playerData.OverrideMorph.Value ~= "nil" then
		chosenMorph = findChildInDescendants(game:GetService("ReplicatedStorage"):WaitForChild("Morphs"):GetDescendants(), playerData.OverrideMorph.Value)
	elseif playerData.OverrideMorph.Value == "nil" then
		chosenMorph = findAppropriateMorph(player)
	end


	addMorph(player.Character, chosenMorph)
	addTools(player, chosenMorph)
end

return morphHandler

Here’s the console log for giving the morph:

  15:37:14.709  nil  -  Server - MorphHandler:192 -- foundMorph
  15:37:14.709  nil  -  Server - MorphHandler:193 -- appropriate Morph
  15:37:14.709  255  -  Server - MorphHandler:194 -- rankId
  15:37:14.709  Powerbase  -  Server - MorphHandler:195 -- team
  15:37:14.710  8732964  -  Server - MorphHandler:196 -- GroupId

The script freaks out when you are in multiple “divisions” [groups].

2 Likes