--//Main script

script.Parent:WaitForChild("ClickDetector").MouseClick:connect(function(Player)
	if not InUse and game:GetService("Players"):FindFirstChild(Player.Name) and Player.Character.Humanoid.RigType == Enum.HumanoidRigType.R15 and Player.Character.Humanoid.Health > 0 and not Player.Character:FindFirstChild("GotCostume") then
		InUse = true
		Player:ClearCharacterAppearance()
		local CostumeLabel = Instance.new("BoolValue", Player.Character)
		CostumeLabel.Name = "GotCostume"
		LockPlayer(Player, true)
		local OldCFrame = Player.Character:FindFirstChild("HumanoidRootPart").CFrame
		Player.Character:SetPrimaryPartCFrame(script.Parent.Parent:FindFirstChild("Position").CFrame)
		for _,BodyPart in pairs(Player.Character:GetChildren()) do
			if BodyPart:IsA("BasePart") and CheckName(BodyPart) then
				if BodyPart.Name == "Head" then
					BodyPart.Transparency = 0.99
				else
					BodyPart.Transparency = 1
				end
				local Face = BodyPart:FindFirstChildOfClass("Decal")
				if Face then
					Face:Destroy()
				end
				local CostumeObject = script.Parent.Parent.Parent.Costume:FindFirstChild(BodyPart.Name)
				if CostumeObject then
					local ClonedCostumeObject = CostumeObject:Clone()
					ClonedCostumeObject.Parent = Player.Character
					for _,CostumeObjectPart in pairs(ClonedCostumeObject:GetChildren()) do
						if CostumeObjectPart:IsA("BasePart") then
							CostumeObjectPart.CustomPhysicalProperties = PhysicalProperties.new(0, 0, 0, 0, 0)
							AddWeld(BodyPart, CostumeObjectPart)
							CostumeObjectPart.Anchored = false
							CostumeObjectPart.CanCollide = false
							CostumeObjectPart.Locked = true
						end
					end
				else
					warn("A piece of costume is missing:", BodyPart.Name)
				end
			end
			wait()
		end
		Player.Character:SetPrimaryPartCFrame(OldCFrame)
		LockPlayer(Player, false)
		InUse = false
	end
end)
